Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::function with void return type and templated parameter

Tags:

c++

templates

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.

like image 920
peku33 Avatar asked Dec 27 '15 17:12

peku33


1 Answers

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;
};
like image 96
Jarod42 Avatar answered Oct 24 '22 16:10

Jarod42