In below code snippet while calling call back function "Invalid use of void expression" error is flashed by the compiler.
#include <iostream>
#include <functional>
using namespace std;
template<class type>
class State {
public:
State(type type1,const std::function<void (type type1 )> Callback)
{
}
};
template <class type>
void Callback(type type1 )
{
//Based on type validation will be done here
}
int main()
{
State<int> obj(10,Callback(10));
return 0;
}
Just want to know what is the wrong here so that same can be addressed .
It seems that you want to pass the Callback<int>
function itself, not its return value (which there is none), to the constructor of obj
. So do just that:
State<int> obj(10, Callback<int>);
Your current code actually calls Callback(10)
first and then tries to take its void
"return value" to pass it to the constructor of obj
. Passing void
is not allowed in C++, which is why the compiler is complaining. (Callback(10)
is the "void expresson" here.)