Is sizeof(int()) a legal expression?

前端 未结 1 1133
南旧
南旧 2021-01-07 19:12

This question is inspired by Is sizeof(void()) a legal expression? but with an important difference as explained below.

The expression in question is:



        
相关标签:
1条回答
  • 2021-01-07 19:25

    No, sizeof( int() ) is ill-formed because int() is taken to be a type-id. Specifically, it's a function type, and sizeof cannot be applied to a function type.

    [dcl.ambig.res]/2:

    An ambiguity can arise from the similarity between a function-style cast and a type-id. The resolution is that any construct that could possibly be a type-id in its syntactic context shall be considered a type-id.

    with this exact example given:

    void foo(signed char a) {
        sizeof(int());                // type-id (ill-formed)
        sizeof(int(a));               // expression
        sizeof(int(unsigned(a)));     // type-id (ill-formed)
    
    0 讨论(0)
提交回复
热议问题