What does void () mean in decltype (void ()) exactly?

This is a continuation of this question, more precisely from the comments of this answer.

What does void() in decltype(void()) mean?
Does it represent a function type, expression, or something else?

+18
c ++ language-lawyer c ++ 11 void decltype
Sep 01 '16 at 19:03
source share
2 answers

Using the C ++ grammar hyperlink , parsing decltype(void()) :

 decltype( expression ) decltype( assignment-expression ) decltype( conditional-expression ) 

... there are many steps related to the order of operations ...

 decltype( postfix-expression ) decltype( simple-type-specifier ( expression-listopt ) ) decltype( void() ) 

So void() is a kind of expression , in particular postfix-expression .

In particular, citing section 5.2.3 [expr.type.conf] paragraph 2 of the 2011 ISO C ++ standard:

The expression T() , where T is a simple type specifier or typename-specifier for an object type without an array or a type (possibly cv-qualit) void , creates a character value of the given type, which is initialized with a value (8.5; no initialization is done for the case of void() ).

So void() is an expression of type void , just as int() is an expression of type int (with a value of 0 ). Obviously, the void expression does not matter, but here it is the decltype operand, so it is not evaluated. decltype refers only to the type of operand, and not to its value.

decltype(void()) is just a detailed way to access the void type.

+9
Sep 01 '16 at 19:52
source share

I quote a comment by @JoachimPileborg, which seems to explain this correctly:

I think I figured it out now, an expression, not a type, is required to describe decltype. void () is not actually a type here, but the C-style cast expression (like int (12.34) for example) void (void) is not an expression, so it doesn't work. How the compiler parses different things depends on the context when it expects a type that it parses as a type, when it expects an expression that it parses as an expression. sizeof () (with parentheses) expects the type first, otherwise it is parsed as an expression in parentheses.

I am not looking for loans or a reputation. In any case, I think it was an interesting answer in the answer, which is worth a dedicated question for future readers.

+4
Sep 01 '16 at 19:03
source share



All Articles