Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass 'void' as a function parameter

I have a function which should accept a parameter of any type. Therefore I use templates.

template <typename T>
void Function(T Parameter);

The function calls a function. In my real application, there is a map of callbacks with string keys, but that doesn't matter for this question. The callback must be a function pointer with return type void, but any parameter type.

void* Callback;

template <typename T>
void Function(T Parameter)
{
    (function<void(T))Callback(Parameter);
}

Assuming that the callback is of the right type, this should work like the following.

Function<int>(42);

// should result in...
(function<void(int))Callback(42);

But in some cases I want to pass void as parameter.

Function<void>(void);

// should result in...
(function<void(void)>Callback(void);

As you can see, I need to provide nothing or void as a parameter. But I cannot pass void as argument. There is an error that the typename would be incorrect.

How can I pass void as an function argument?

like image 850
danijar Avatar asked Dec 01 '12 19:12

danijar


1 Answers

Just specify a non-template overload:

void* Callback;

template <typename T>
void Function(T Parameter)
{
    (function<void(T)>)Callback(Parameter);
}

void Function()
{
    (function<void()>)Callback();
}
like image 181
Adam Maras Avatar answered Oct 11 '22 00:10

Adam Maras