BK C.
BK C.

Reputation: 583

typename keyword for std::function

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

Answers (2)

Some programmer dude
Some programmer dude

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

Const
Const

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

Related Questions