28

Take the following minimal example:

using Type1 = std::function<void(void)>;

template <typename T>
using Type2 = std::function<void(T)>;

Type1 whyDoesThisWork;
Type2<void> andYetThisDoesNot;

If the second type alias, I get the error "Argument may not have 'void' type". (I tested with Xcode 4.5, Clang/c++11/libc++, OS X 10.7.)

I find this curious: I would have expected Type1 and Type2<void> to behave identically. What's going on here? And is there a way to rewrite the second type alias so I can write Type2<void> and get std::function<void(void)> instead of an error?

Edit I should probably add that the reason I want this is to allow for something like the following:

template <typename ... T>
using Continuation = std::function<void(T...)>;

auto someFunc = []() -> void {
  printf("I'm returning void!\n");
};

Continuation<decltype(someFunc())> c;

Continuation<decltype(someFunc())> becomes Continuation<void> and I get the error.

Nick Hutchinson
  • 5,044
  • 5
  • 33
  • 34

5 Answers5

15

I don't have an actual answer, only what I said in the comment: You can't have void as a function type, as in:

int foo(int, char, void, bool, void, void);     // nonsense!

I believe that T(void) is only allowed as a compatibility notation for C (which distinguishes declarations and prototypes, very differently from C++, and which needs to be able to say "no arguments").

So, the solution should be variadic:

template <typename ...Args> using myType = std::function<void(Args...)>;

That way you can properly have no arguments:

myType<> f = []() { std::cout << "Boo\n"; }
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    Sorry, I somehow managed to down-vote this by accident and didn't notice until after it was too late to change it. – Gerald Nov 07 '13 at 18:17
15

The short answer is "templates are not string substitution". void f(void) has meaning only so far as it is an alias for void f() in C++, in order to be backwards compatible with C.

The first step is to use variadics, as noted elsewhere.

The second step is figuring out how to map void returning functions to ... well, maybe something like std::function<void()>, or maybe something else. I say maybe something else because unlike the other cases, you cannot call std::function<void()> foo; foo( []()->void {} ); -- it isn't a true continuation.

Something like this maybe:

template<typename T>
struct Continuation
{
  typedef std::function<void(T)> type;
};

template<>
struct Continuation<void>
{
  typedef std::function<void()> type;
};

then use it like this:

auto someFunc = []()->void {};
Continuation<decltype(someFunc())>::type c;

which gives you the type you want. You could even add in an apply to continuation:

template<typename T>
struct Continuation
{
  typedef std::function<void(T)> type;

  template<typename func, typename... Args>
  static void Apply( type const& cont, func&& f, Args... args)
  {
    cont( f(args...) );
  }
};

template<>
struct Continuation<void>
{
  typedef std::function<void()> type;
  template<typename func, typename... Args>
  static void Apply( type const& cont, func&& f, Args... args)
  {
    f(args...);
    cont();
  }
};

which lets you apply a continuation to an execution of a function uniformly if the incoming type is a void or if it is a non-void type.

However, I would ask "why would you want to do this"?

Donal Lafferty
  • 5,807
  • 7
  • 43
  • 60
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Thanks, that pointed me in the right direction. _Why_ would I want do do this? I'm trying to implement a simple version of Futures from Microsoft's [Task Parallel Library](http://msdn.microsoft.com/en-us/library/ff963556.aspx). A continuation receives as arguments the return values of the tasks it depends on. – Nick Hutchinson Nov 15 '12 at 08:57
9

Several answers already explain the rationale. To add to those answers, the specification says (C++11 §8.3.5[dcl.func]/4):

A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list. Except for this special case, a parameter shall not have type cv void.

In your Type2 example, the T in void(T) is a dependent type--it depends on a template parameter.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • This also means that non-dependent types like `void(typename std::type_identity::type)` and `void(std::void_t)` also work which is strange – Artyer Aug 18 '20 at 19:02
3

When a function is declared to take a parameter of type void, as in std::function<void(void)>, that is really just a goofy way of saying that it takes zero parameters. But the way you've declared Type2 is as a std::function with a signature that returns nothing (void), but that takes 1 parameter. void is not a type that can be used as a parameter, it is just a way of declaring that there are no parameters. So it doesn't work with Type2, because that requires an actual type that can be used as a parameter.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
0

Void can be interpreted as an empty parameter if you pass it to a function. You're not using a void pointer after all so

void func (void)

becomes

void func ()
count0
  • 2,537
  • 2
  • 22
  • 30