0

Let's say I have a type alias like this:

template <typename Arg>
using Func = std::function<void(Arg)>;

It works fine except for the case when Arg is void:

Func<int> f1;
Func<void> f2; // doesn't compile

The second one gives the following compilation error:

error: invalid parameter type ‘void’using Func = std::function<void(Arg)>;
error: in declaration ‘using Func = class std::function<void(Arg)>’

How can I make it work to create alias for std::function<void()> ?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Alexandre A.
  • 1,619
  • 18
  • 29

1 Answers1

2

You could try to add some template specializations:

template<typename T>
struct FuncImpl
{
    using type = std::function<void(T)>;
};

template<>
struct FuncImpl<void>
{
    using type = std::function<void()>;
};

template <typename Arg>
using Func = typename FuncImpl<Arg>::type;

EXAMPLE

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
  • Thanks! It does the trick (answer accepted) but is there an explanation ? – Alexandre A. Jul 25 '18 at 07:42
  • @AlexandreA. Hm, I have just done some search and seems that the question is dupe of: https://stackoverflow.com/questions/13372173/using-void-template-arguments-in-c. Unfortunately, I checked it after I had posted the answer :( – Edgar Rokjān Jul 25 '18 at 07:47
  • Indeed but thanks anyway to pointing out the right direction! – Alexandre A. Jul 25 '18 at 07:58