std::bind and variadic template function - c++

Is this even possible?
#include <iostream>
#include <functional>
enum class Enum {a, b, c };
class Dispatch {
public:
void check(uint16_t) { std::cout << "check 16\n"; }
void check(uint32_t) { std::cout << "check 32\n"; }
void check(uint64_t) { std::cout << "check 64\n"; }
template<Enum E, typename... A>
void event(A&&... args) {
tag_event(Tag<E>(), std::forward<A>(args)...);
}
private:
template<Enum E> struct Tag {};
void tag_event(Tag<Enum::a>, uint16_t) { std::cout << "a\n"; }
void tag_event(Tag<Enum::b>, uint16_t) { std::cout << "b\n"; }
void tag_event(Tag<Enum::c>, uint16_t) { std::cout << "c\n"; }
};
void exec(std::function<void()>&& func) { func(); }
int main() {
Dispatch d;
// all good
exec(std::bind(static_cast<void(Dispatch::*)(uint16_t)>(&Dispatch::check), &d, uint16_t()));
exec(std::bind(static_cast<void(Dispatch::*)(uint32_t)>(&Dispatch::check), &d, uint32_t()));
exec(std::bind(static_cast<void(Dispatch::*)(uint64_t)>(&Dispatch::check), &d, uint64_t()));
// all good
d.event<Enum::a>(uint16_t());
d.event<Enum::b>(uint16_t());
d.event<Enum::c>(uint16_t());
// but how do we bind an event<> call?
exec(std::bind(static_cast<void(Dispatch::*)(uint16_t)>(&Dispatch::event<Enum::a>), &d, uint16_t()));
}
So I'm trying to bind a call to the variadic template method but get the following compiler error...
In function 'int main()':
42:86: error: no matches converting function 'event' to type 'void (class Dispatch::*)(uint16_t) {aka void (class Dispatch::*)(short unsigned int)}'
13:10: note: candidate is: template<Enum E, class ... A> void Dispatch::event(A&& ...)
Any suggestions short of exposing all the tag overloads instead?

I suggest to pass through a lambda function, as suggested in comments.
Anyway, if you want to pass to std::bind(), seems to me that a possible solution is
// ..................................................VVVVVVVV <-- ad this
exec(std::bind(static_cast<void(Dispatch::*)(uint16_t const &)>
(&Dispatch::event<Enum::a, uint16_t const &>), &d, uint16_t()));
// ...........................^^^^^^^^^^^^^^^^^^ <-- and this
I mean: you have to select the event() method explicating also the received type; I suggest uint16_t const & (instead of uint16_t) that is compatible with universal reference signature of your event() method (I suppose other combinations are possible but for a uint16_t activate move semantics... I suppose it's superfluous).

Related

C++: use void as template argument

I have this minimal class to represent an event which client can subscribe to.
The event can have an data type associated to it, so when it is triggered by a publisher, an argument of that type would be passed to the client's callback:
template<typename Arg, typename Callback = function<void(const Arg&)>>
class Event
{
public:
Event(Callback c) : mCallback(c){}
void Trigger(const Arg& arg) {
mCallback(arg);
}
private:
Callback mCallback;
};
Now I can create an Event<int> or any other concrete type, but it is really important to me to also allow "empty" event, which has no data associated with it: Event<void>
But sadly that doesn't work:
static void FooVoid() {
cout << "Look ma, no args!" << endl;
}
static void FooInt(int a) {
cout << "int arg " << a << endl;
}
int main()
{
/* Compiles */
Event<int> eInt(&FooInt);
eInt.Trigger(42);
/* Does not compile :(
Event<void> eVoid(&FooVoid);
eVoid.Trigger();
*/
return 0;
}
Is there any way to achieve this desired API? How?
(P.S the solution should work on C++11)
The quickest way of solving this without explicitly specializing for void is to use a parameter pack (added in C++11) for your template argument instead of a single type and using an empty parameter pack instead of void. A parameter pack can homogeneously hold any number of type, including 0 and 1. Then it can be used to generate the right types and member functions. You basically just have to add ... correctly near every use of Arg (link) :
#include <functional>
#include <iostream>
template<typename ... Arg>
class Event
{
public:
using Callback = std::function<void(const Arg&...)>;
Event(Callback c) : mCallback(c){}
void Trigger(const Arg& ... arg) {
mCallback(arg...);
}
private:
Callback mCallback;
};
static void FooVoid() {
std::cout << "Look ma, no args!" << std::endl;
}
static void FooInt(int a) {
std::cout << "int arg " << a << std::endl;
}
int main()
{
/* Compiles */
Event<int> eInt(&FooInt);
eInt.Trigger(42);
Event<> eVoid(&FooVoid);
eVoid.Trigger();
return 0;
}
This has the added benefit that you can use callbacks with more than one argument. If this isn't desirable you can add a static_assert to prevent it :
template<typename ... Arg>
class Event
{
public:
using Callback = std::function<void(const Arg&...)>;
static_assert(sizeof...(Arg) <= 1, "Too many arguments");
Event(Callback c) : mCallback(c){}
void Trigger(const Arg& ... arg) {
mCallback(arg...);
}
private:
Callback mCallback;
};
Notice that this solution requires Event<> instead of Event<void>. You can solve that by adding a short specialization for Event<void> that uses Event<> (link) :
template<>
class Event<void> : public Event<>
{
// Inherit constructors
using Event<>::Event;
};

How to pass a pointer to method as a parameter?

I want to pass a method as an argument to a method which takes an int and returns void:
void A::SetCallback(void (*callback)(int))
{
.....................
}
void B::test()
{
a->SetCallback(&B::Done); //
}
void B::Done(int i)
{
..........................
}
Inside test() I get this error:
Error 1 error C2664: cannot convert parameter 1 from 'void (__thiscall B::* )(int)' to 'void (__cdecl *)(int)'
I saw some example on StackOverflow how to fix this but it uses elements from C++11, to which I do not have access.
How can I fix this using C++03 ?
You cannot pass a non-static method to a function that takes a pointer to a function. Functions and methods are two completely different things. In order to invoke a non-static method you have to, obviously, have an object whose method you're invoking.
If, in your example, Done() is a static class method, then, yes, you can pass it this way, since a static class method is just another name for a function.
It is possible to have a pointer to a class method:
void A::SetCallback(void (B::*callback)(int))
{
}
void B::test()
{
a->SetCallback(&B::Done); //
}
void B::Done(int i)
{
..........................
}
But in order to invoke the class method, you need an object whose method to invoke:
B *object=give_me_a_pointer_to_b_from_somewhere();
(object->*callback)(0);
&ClassName::method_name creates the pointer and void(ClassName::*)(int, char*) is the type.
void go(void(ClassName::*parameter_name)(int, char*));
go(&ClassName::method_name);
You cannot pass a pointer to class method (which is of type void (B::*) (int) in your case) to a pointer to a free function.
What would happen if you could?
void f (void (*g) (int)) {
g(2);
}
struct A {
int x;
void foo (int c) { x += c; }
};
f(&A::foo); // Oh oh! Where will I find `x` in the call `g(2)`?
If you only want pointer to method of B, you need to change the callback type:
void A::SetCallback(void (B::*callback)(int)) { }
But then you need an instance of B to call your callback, e.g.:
B b;
(b.*callback)(2);
I'm using following code. It is not pretty, but you asking for C++03:
#include <iostream>
#include <vector>
#include <functional>
class AbstractCallback {
public:
virtual void call(int arg) = 0;
};
template <class T>
class Callback : public AbstractCallback {
public:
typedef std::mem_fun1_t<void, T, int> CallbackFunc;
private:
CallbackFunc func;
T* object;
public:
Callback(T* _object, const CallbackFunc& _func)
: object(_object), func(_func) {
}
void call(int arg) {
func(object, arg);
}
};
struct A {
void foo(int a) {
std::cout << "foo " << a << std::endl;
}
};
struct B {
void bar(int a) {
std::cout << "bar " << a << std::endl;
}
};
int main() {
A a;
B b;
AbstractCallback* cbs[2] = {
new Callback<A>(&a, std::mem_fun(&A::foo)),
new Callback<B>(&b, std::mem_fun(&B::bar)),
};
cbs[0]->call(10);
cbs[1]->call(22);
delete cbs[0];
delete cbs[1];
return 0;
}
As you can see pointer-to member functions (of type A::* and B::*) are wrapped into std::mem_funs and a Callback class which is generated for each type (A and B in this case).
This allows to keep method of any type in vectors, arrays or lists of abstract callbacks.

How to use variadic parameter in lambda from variadic template class

template<typename FirstArgT, typename...ArgsT>
class Server :public Server<ArgsT...> {
public:
Server(const function<void (FirstArgT, ArgsT...)>& func)
:Server<ArgsT...>([&](ArgsT args...) -> void { func(arg0, args...); }) { }
private:
FirstArgT arg0;
}
but the compiler says :
Error C3520 'ArgsT': parameter pack must be expanded in this context
Error C3546 '...': there are no parameter packs available to expand
in line 4 and 5.
Is it possible to use variadic parameters as parameters of a lambda is VS2015, or is there an alternative way to do it?
I extended and fixed your code to get it compiled. It would be nice if your next question comes with full example so that we have not the need to extend the rest of the example ;)
Indeed, I have no idea what you code is good for :-)
template<typename ...T> class Server;
template<typename FirstArgT, typename...ArgsT>
class Server<FirstArgT,ArgsT...> :public Server<ArgsT...> {
public:
Server(const std::function<void (FirstArgT, ArgsT...)>& func)
:Server<ArgsT...>([&](ArgsT ... args)-> void { func(arg0, args...); }) { }
private:
FirstArgT arg0;
};
template<typename FirstArgT>
class Server<FirstArgT>
{
public:
Server(const std::function<void (FirstArgT)>& func) {}
};
void Do( int, double) {}
int main()
{
Server<int,double> se( &Do );
}
If your intention is only to store the arguments somewhere and call the function with stored arguments, simply use std::bind.
void Do( int i, double d) { std::cout << i << " " << d << std::endl; }
int main()
{
auto fx= std::bind( &Do, 1, 2.34);
fx();
// which works also for lambda:
auto fx2=
std::bind( []( int i,double d )->void
{ std::cout << i << " " << d << std::endl; }, 4, 5.6);
}

Static polymorphism in C++

#include <iostream>
template<typename Impl>
struct renderer{
void get(){
static_cast<Impl*>(this)->get();
}
};
struct open_gl : public renderer<open_gl>{
void get(){
std::cout << "OpenGL" << std::endl;
}
};
struct direct_draw : public renderer<direct_draw>{
void get(){
std::cout << "DX" << std::endl;
}
};
template<typename T>
void print_renderer(renderer<T> r){
r.get();
}
int main() {
auto gl = open_gl();
auto dx = direct_draw();
print_renderer(gl);
print_renderer(dx);
}
Why can't I change the parameter of print_renderer to void
print_renderer(const renderer<T> &r)?
cannot convert 'this' pointer from 'const renderer<open_gl>' to 'renderer<open_gl> &'
`
Why do I get a runtime error when I rename the method get in open_gl from
get to get1? Shouldn't this trigger a compiler error? Error = Stack overflow
**Note I am using the latest MSVC
1) Because get is not a const member function : it cannot make the promise of not modify your (const) argument.
You could declare get as const, and it compiles fine :
void get() const { ... }
2) The base get method will be called, going into infinite recursion : Stack Overflow.
If you declare your function override (it needs to be virtual), the compiler will throw an error if it does not indeed override a base method :
void get1() override { ... } // Compiler error
void get() override { ... } // Ok
Note:
The title is "Static polymorphism in C++", but I think that you misunderstood what is static polymorphism : it does not (have to) make use of inheritance (as you did). Rather, the templates compile-time duck typing will statically "resolve" function calls for you.
That is, you don't need related types, you don't need the base renderer class at all, and you can simply do the following (in which case, renaming to get1 will cause a compiler error) :
#include <iostream>
struct open_gl {
void get(){
std::cout << "OpenGL" << std::endl;
}
};
struct direct_draw {
void get(){
std::cout << "DX" << std::endl;
}
};
template<typename T>
void print_renderer(T r){
r.get();
}
int main() {
auto gl = open_gl();
auto dx = direct_draw();
print_renderer(gl);
print_renderer(dx);
}
Live demo
Becuase get is not marked const.
Because the base class method is used (irrelevantly of cast), and it goes into infinite loop.

c++ generic pointer to (member?) function

I can't seem to declare a generic pointer to function.
Having these 2 functions to be called:
void myfunc1(std::string str)
{
std::cout << str << std::endl;
}
struct X
{
void f(std::string str){ std::cout<< str << std::endl;}
};
and these two function callers:
typedef void (*userhandler_t) (std::string);
struct example
{
userhandler_t userhandler_;
example(userhandler_t userhandler): userhandler_(userhandler){}
void call(std::string str)
{
userhandler_(str);
}
};
template<typename func_t>
void justfunc(func_t func)
{
func("hello, works!");
}
when I try to use them with boost::bind to call the member function they give me compile errors.
this works:
example e1(&myfunc1);
e1.call("hello, world!");
justfunc(&myfunc1);
this doesn't:
X x;
example e2(boost::bind(&X::f, &x, _1));
e2.call("hello, world2!");
justfunc(boost::bind(&X::f, &x, _1));
How is this supposed to be done?
boost::bind creates objects that behave like functions, not actual function pointers. Use the Boost.Function library to hold the result of calling boost::bind:
struct example
{
boost::function<void(std::string)> userhandler_;
...
};