peku33
peku33

Reputation: 3903

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.

Upvotes: 5

Views: 2537

Answers (1)

Jarod42
Jarod42

Reputation: 217275

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;
};

Upvotes: 7

Related Questions