You can turn the function to a template one, then use a static_assert
and std::is_void
from type_traits
:
template<typename T>
int foo(T *ptr) {
static_assert(std::is_void<T>::value, "!");
// ....
}
Otherwise, you can use a std::enable_if_t
on the return type:
template<typename T>
std::enable_if_t<std::is_void<T>::value, int>
foo(T *ptr) {
// ....
return 0;
}
And so on, other interesting solutions have already been proposed by other users with their answers.
Here is a minimal, working example:
#include<type_traits>
template<typename T>
int foo(T *ptr) {
static_assert(std::is_void<T>::value, "!");
// ....
return 0;
}
int main() {
int i = 42;
void *p = &i;
foo(p);
// foo(&i); // compile error
}