I was given a piece of code that uses void()
as an argument. The code doesn't compile... obviously?
Can we instantiate anything of type void
? I believed the answer was no, with the exception of a void*
. For example:
- Writing the function
void askVoid(void param) {}
errors:
A parameter may not have void
type
- Writing the function
void askNaught() {}
and calling it with askNaught(void())` errors:
error C2660: takeNaught
: function does not take 1 arguments
- Writing the templatized function
template <typename T> void takeGeneric(T param) {}
and calling it with takeGeneric(void())
errors:
error C2893: Failed to specialize function template void takeGeneric(T)
- Declaring
void voidType
errors:
Incomplete type is not allowed
- Declaring
auto autoVoid = void()
errors:
Cannot deduce auto
type
- Declaring
void* voidPtr
works fine, but remove_pointer_t<decltype(voidPtr)> decltypeVoid
errors:
error C2182: decltypeVoid
: illegal use of type void
That's it, right? There is no place for void()
in C++ is there? This is just bad code I've been given, right?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…