Reputation: 583
I have a class
class Consumer
{
public:
typedef std::function<void()> EventHandler;
...
};
which I would like to use in this as template
template<class Consumer>
class ConsumerGroup
{
public:
typename Consumer::EventHandler EventHandler;
ConsumerGroup(EventHandler handler);
};
But above one results compilation error saying EventHandler is not a type. How should I use typename keyword in this case?
Upvotes: 1
Views: 703
Reputation: 409166
The typename
keyword isn't used to import or define new type-aliases. What you're doing is really defining a member variable named EventHandler
.
You need to use typdef
again to define a type-alias:
typedef typename Consumer::EventHandler EventHandler;
Or with modern C++ using using
:
using EventHandler = typename Consumer::EventHandler;
Upvotes: 2
Reputation: 1356
You need a type alias for dependent Consumer::EventHandler
.
This should work:
using EventHandler = typename Consumer::EventHandler;
for lower compiler versions(before C++11)
typedef typename Consumer::EventHandler EventHandler;
Upvotes: 3