What does "void ()" mean; how does a separate operator mean in C ++?

How to compile this program?

int main() { void(); // Does this create a "void" object here? } 

I tested in both MSVC and GCC. But void is an incomplete type. If you do the same for any other incomplete user type,

 class Incomplete; int main() { Incomplete(); // Error saying "Incomplete" is incomplete. } 
+5
source share
3 answers

void type is and has always been special. This is really incomplete, but is allowed in many contexts where the full type is usually expected. Otherwise, for example, the definition of the void function would be invalid due to the incompleteness of the void type. It is also possible to write expressions of the void type (any call to the void function is an example of such an expression).

Even in C, you can use direct expressions of type void type (void) 0 . What you have in your code is just an example of a C ++ syntax that does almost the same thing: it creates a no-op expression of type void .

+3
source

C ++ 11 ยง5.2.3 [expr.type.conv] / 2 is examined in detail (emphasized by me):

The expression T (), where T is a simple type specifier or typename-specifier for an object type without an array or (possibly cv-qualified) type void, creates a value of the given type value, the value of which is the initialization of the value (8.5) of an object of type T; initialization is not performed for the case of void () .

This is just a void value. No special initialization or anything like int() . PRvalue is something like true , or nullptr , or 2 . The expression is equivalent to 2; but for void instead of int .

+11
source

In C ++, the creation of the void type is allowed as an argument to the template.

+2
source

Source: https://habr.com/ru/post/1202544/


All Articles