简体   繁体   中英

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 >.

In this case compiler expands typedef to

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

and does not want to cooperate.

If void is used as R parameter I want typedef to behave like:

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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