繁体   English   中英

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

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

我有一个

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

在我尝试使用MyClass <void>之前一切正常。

在这种情况下,编译器将typedef扩展为

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

而且不想合作。

如果void用作R参数,我希望typedef的行为如下:

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

由于类非常大,我更喜欢type_traits和enable_if-like,而不是为void创建特化。

如评论中所述,您可以使用帮助程序类:

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

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

然后,在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