Different meanings of parentheses in C++?

前端 未结 3 1985
粉色の甜心
粉色の甜心 2020-12-29 12:49

I am a bit confused withnthe interpretation of parentheses by the compiler. Can some one please explain what actually happens in such contexts?

Casting: (int)a

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 13:11

    From C++14 Appendix A, the complete list of cases where parentheses may appear in the grammar is:

    §A.14 Preprocessing directives
    control-line: # define identifier lparen identifier-list_opt ) replacement-list new-line
    control-line: # define identifier lparen ... ) replacement-list new-line
    control-line: # define identifier lparen identifier-list , ... ) replacement-list new-line
    
    §A.2 Lexical conventions
    raw-string: " d-char-sequence_opt ( r-char-sequence_opt ) d-char-sequence_opt "
    
    §A.4 Expressions
    primary-expression: ( expression )
    lambda-declarator: ( parameter-declaration-clause ) mutable_opt exception-specification_opt attribute-specifier-seq_opt trailing-return-type_opt
    postfix-expression: const_cast < type-id > ( expression )
    postfix-expression: dynamic_cast < type-id > ( expression )
    postfix-expression: postfix-expression ( expression-list_opt )
    postfix-expression: reinterpret_cast < type-id > ( expression )
    postfix-expression: simple-type-specifier ( expression-list_opt )
    postfix-expression: static_cast < type-id > ( expression )
    postfix-expression: typeid ( expression )
    postfix-expression: typeid ( type-id )
    postfix-expression: typename-specifier ( expression-list_opt )
    unary-expression: alignof ( type-id )
    unary-expression: sizeof ( type-id )
    unary-expression: sizeof ... ( identifier )
    new-expression: ::_opt new new-placement_opt ( type-id ) new-initializer_opt
    new-placement: ( expression-list )
    new-initializer: ( expression-list_opt )
    noexcept-expression: noexcept ( expression )
    cast-expression: ( type-id ) cast-expression
    
    §A.5 Statements
    selection-statement: if ( condition ) statement
    selection-statement: if ( condition ) statement else statement
    selection-statement: switch ( condition ) statement
    iteration-statement: do statement while ( expression ) ;
    iteration-statement: for ( for-init-statement condition_opt ; expression_opt ) statement
    iteration-statement: for ( for-range-declaration : for-range-initializer ) statement
    iteration-statement: while ( condition ) statement
    
    §A.6 Declarations
    static_assert-declaration: static_assert ( constant-expression , string-literal ) ;
    decltype-specifier: decltype ( auto )
    decltype-specifier: decltype ( expression )
    asm-definition: asm ( string-literal ) ;
    alignment-specifier: alignas ( assignment-expression ..._opt )
    alignment-specifier: alignas ( type-id ..._opt )
    attribute-argument-clause: ( balanced-token-seq )
    balanced-token: ( balanced-token-seq )
    
    §A.7 Declarators
    noptr-declarator: ( ptr-declarator )
    parameters-and-qualifiers: ( parameter-declaration-clause ) attribute-specifier-seq_opt cv-qualifier-seq_opt ref-qualifier_opt exception-specification_opt
    noptr-abstract-declarator: ( ptr-abstract-declarator )
    initializer: ( expression-list )
    
    §A.10 Special member functions
    mem-initializer: mem-initializer-id ( expression-list_opt )
    
    §A.11 Overloading
    operator-function-id: operator ( )
    
    §A.13 Exception handling
    handler: catch ( exception-declaration ) compound-statement
    dynamic-exception-specification: throw ( type-id-list_opt )
    noexcept-specification: noexcept ( constant-expression )
    

    Note that:

    • The preprocessor rules for if-group and elif-group do refer to constant-expression.
    • lparen means a ( with no preceding whitespace
    • The rule for raw-string is during lexing, so the ( and ) do not become tokens.
    • Any sequence of valid tokens can appear in a preprocessor group whose condition evaluates to false.

    In your question, you use the following:

    • cast-expression: ( type-id ) cast-expression
    • postfix-expression: simple-type-specifier ( expression-list_opt )
    • parameters-and-qualifiers: ( parameter-declaration-clause ) attribute-specifier-seq_opt cv-qualifier-seq_opt ref-qualifier_opt exception-specification_opt
    • noptr-abstract-declarator: ( ptr-abstract-declarator )

提交回复
热议问题