[cfe-dev] Semantics of Value Dependent and Integral Constant Expressions

Davis, Matthew via cfe-dev cfe-dev at lists.llvm.org
Thu Nov 16 18:49:16 PST 2017


I have been investigating bug https://bugs.llvm.org/show_bug.cgi?id=12779, which has led me to a question regarding the semantics behind what it means to be a Value Dependent Expression and their relations to Integral Constant Expressions (ICE).

Is the constant expression: sizeof(T), where T is a template type argument, considered an ICE?  It seems to me that it is an ICE.  There is nothing in the C++11 specification which would have me believe otherwise.

The aforementioned bug triggers an assertion in Clang's frontend, specifically in Decl.cpp:VarDecl::checkInitIsICE().  That routine decides if a declaration is an ICE under the constraint that the initializer of the variable is non-value dependent.  In fact, the compiler expects to never handle the value-dependent case, as implied by the assertion.  In legal template code this assertion would/should never trigger as the template type argument would have been expanded.  But, in malformed code (e.g., during error recovery) that assertion can be triggered.

What confuses me is that the specification considers sizeof(type-id) to be a value dependent expression, even though it appears that it is an ICE.
[temp.dep.constexpr]:
Expressions of the following form are value-dependent if the unary-expression or expression is type-dependent or the type-id is dependent:
sizeof unary-expression
sizeof ( type-id )
[clip]

In the case of sizeof(T), where T is a template type argument, T is dependent; therefore, sizeof(T) is considered a value dependent constant expression.  Why is sizeof(T) disallowed from being an ICE in checkInitIsICE()? I realize that the test case referenced in the bug (comment #6) is syntactically malformed and that the compiler expects to never see this case.  

Thanks,
-Matt



More information about the cfe-dev mailing list