Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c++ - What does the void() in decltype(void()) mean exactly?

This is a follow-up of this question, more precisely of the comments of this answer.

What does the void() in decltype(void()) represent exactly?
Does it represent a function type, an expression or whatever?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using a hyperlinked C++ grammar, the parsing of decltype(void()) is:

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

... lots of steps involving order of operations go here ...

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

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

Specifically, quoting 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 a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, which is value-initialized (8.5; no initialization is done for the void() case).

So void() is an expression of type void, just as int() is an expression of type int (with value 0). Clearly a void expression has no value, but here it's the operand of decltype, so it's not evaluated. decltype refers only to its operand's type, not its value.

decltype(void()) is simply a verbose way of referring to the type void.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...