Declarations/definitions as statements in C and C++

后端 未结 4 1970
遇见更好的自我
遇见更好的自我 2021-02-03 18:28

I was confused when this wouldn\'t compile in C:

int main()
{
    for (int i = 0; i < 4; ++i)
        int a = 5; // A dependent statement may not be declarati         


        
4条回答
  •  一生所求
    2021-02-03 18:43

    In C++, a statement is (C++17 standard draft)

    excerpt from [gram.stmt]
    
    statement:
        labeled-statement
        attribute-specifier-seqopt expression-statement
        attribute-specifier-seqopt compound-statement
        attribute-specifier-seqopt selection-statement
        attribute-specifier-seqopt iteration-statement
        attribute-specifier-seqopt jump-statement
        declaration-statement
        attribute-specifier-seqopt try-block
    
    init-statement:
        expression-statement
        simple-declaration
    
    declaration-statement:
        block-declaration
    
    ...
    

    Note that there are declaration statements in C++, which are declarations, and are statements. Similarly, simple declarations are init statements. Not all declarations are statements though. The grammar of declarations contains things that are not in the list of statements:

    excerpt from [gram.dcl]
    
    declaration:
        block-declaration
        nodeclspec-function-declaration
        function-definition
        template-declaration
        deduction-guide
        explicit-instantiation
        explicit-specialization
        linkage-specification
        namespace-definition
        empty-declaration
        attribute-declaration
    
    block-declaration:
        simple-declaration
        asm-definition
        namespace-alias-definition
        using-declaration
        using-directive
        static_assert-declaration
        alias-declaration
        opaque-enum-declaration
    
    simple-declaration:
        decl-specifier-seq init-declarator-listopt ;
        attribute-specifier-seq decl-specifier-seq init-declarator-list ;
        attribute-specifier-seqopt decl-specifier-seq ref-qualifieropt [ identifier-list ] initializer ;
    
    ...
    

    The list of declaration grammars continues on for a few pages.


    In C, a statement is (C11 standard draft)

    excerpt from Statements and blocks
    
    statement:
        labeled-statement
        compound-statement
        expression-statement
        selection-statement
        iteration-statement
        jump-statement
    

    Note that there are no declarations that are statements in C.


    So, the meaning of statement is clearly different in the languages. Statement in C++ appears to have a broader meaning than statement in C.

提交回复
热议问题