I'm not sure if it's possible, so that's what I want to find out.
I'd like to create a function which accepts any kind of functor/callable object, but I want to know what the argument type is. ( but not enforce it )
So, this one captures all but doesn't give me the type of the argument:
template < typename T >
void optionA( T );
This one captures most, and has the type of the argument
template < typename T >
void optionB( std::function< void(T) > );
But this one doesn't allow lambdas, so
optionB( [](int){} );
will not compile.
Which is somewhat strange, as this will compile:
std::function< void(int) > func = [](int){};
optionB( func );
So is there a way to accept all options and also know which type of argument is expected?
thanks in advance!
-- edit --
The reason I'd like to do this comes from the fact that I want the user of my library to register a callback with a certain type. To me, the most natural way is
auto callback = []( int val ) { cout << "my callback " << val << endl; };
object.register( callback );
(with or without the use of callback as intermediate variable)
Since I need to modify the behaviour based on the type of value the user expects, I need to know what type he/she expects.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…