[英]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. 但是上面的结果编译错误说EventHandler不是类型。 How should I use typename keyword in this case? 在这种情况下,应如何使用typename关键字?
You need a type alias for dependent Consumer::EventHandler
. 您需要为依赖的Consumer::EventHandler
类型别名。
This should work: 这应该工作:
using EventHandler = typename Consumer::EventHandler;
for lower compiler versions(before C++11) 用于较低的编译器版本(在C ++ 11之前)
typedef typename Consumer::EventHandler EventHandler;
The typename
keyword isn't used to import or define new type-aliases. typename
关键字不用于导入或定义新的类型别名。 What you're doing is really defining a member variable named EventHandler
. 您实际上正在定义一个名为EventHandler
的成员变量 。
You need to use typdef
again to define a type-alias: 您需要再次使用typdef
来定义类型别名:
typedef typename Consumer::EventHandler EventHandler;
Or with modern C++ using using
: 或使用, using
现代C ++:
using EventHandler = typename Consumer::EventHandler;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.