简体   繁体   English

具有void返回类型和模板化参数的std :: function

[英]std::function with void return type and templated parameter

I have a 我有一个

template<class R>
class MyClass
{
    public:
        typedef std::function<void (const R)> ...;
};

Everything is ok until I try to use MyClass < void >. 在我尝试使用MyClass <void>之前一切正常。

In this case compiler expands typedef to 在这种情况下,编译器将typedef扩展为

typedef std::function<void (void)> ...;

and does not want to cooperate. 而且不想合作。

If void is used as R parameter I want typedef to behave like: 如果void用作R参数,我希望typedef的行为如下:

typedef std::function<void ()> ...;

Since the class is pretty big I prefer type_traits and enable_if-like stuff instead of creating specialization for void. 由于类非常大,我更喜欢type_traits和enable_if-like,而不是为void创建特化。

As mentioned in comment, you may use an helper class: 如评论中所述,您可以使用帮助程序类:

template<class R>
struct MyClassHelper
{
    using function_type = std::function<void (const R)>;
};

template <>
struct MyClassHelper<void>
{
    using function_type = std::function<void ()>;
};

And then, in MyClass 然后,在MyClass

template<class R>
class MyClass
{
public:
    using function_type = typename MyClassHelper<R>::function_type;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM