Reputation: 406
I am trying to write a class template that would get two std::function
s in the constructor parameter and call them upon construction and destruction, in a RAII style.
What am I missing?
template <class T>
class HelloGoodbye{
public:
HelloGoodbye(std::function<T> const & f, std::function<T> const &g)
:mf(f),mg(g)
{
mf();
}
~HelloGoodBye()
{
mg();
}
private:
std::function<T> mf;
std::function<T> mg;
};
class Printer
{
public:
void hello()
{
std::cout << "hello!" << std::endl;
}
void goodbye()
{
std::cout << "Goodbye!" << std::endl;
}
};
int main()
{
Printer p;
auto hi = std::bind(&Printer::hello, &p);
auto bye = std::bind(&Printer::goodbye, &p);
HelloGoodbye hgb(hi,bye);
}
Upvotes: 3
Views: 4056
Reputation: 66210
You can explicit the template type of hgb
, as suggested by songyuanyao, or you can create a make function
template <typename T>
HelloGoodbye<T> makeHG (std::function<T> const & hi,
std::function<T> const & bye)
{ return { hi, bye }; }
and, using auto
, you can initialize hgb
as follows
auto hgb = makeHG(hi, bye);
Unfortunately, std::bind
is a strange beast with an undefined return value, so you can't pass the type returned from std::bind
to makeHG()
to obtain the T
type deduction.
So, if you want to use makeHG()
, you can use auto
to define hi
and bye
but you have to explict the type as follows
std::function<void()> hi = std::bind(&Printer::hello, &p);
std::function<void()> bye = std::bind(&Printer::goodbye, &p);
So, I suppose, the sonyuanyao one is a better solution.
Upvotes: 0
Reputation: 172934
HelloGoodbye
is a class template, you need to specify the template argument when use it. e.g.
HelloGoodbye<void()> hgb(hi,bye);
BTW: I suppose ~HelloGoodBye()
is a typo of ~HelloGoodbye()
.
Upvotes: 3