我对编译器对括号的解释有点困惑。有人可以解释一下在这种情况下实际发生了什么吗?
类型转换:(int)a
或者 int(a)
参数传递:
template <typename t>
int size(t (&)[n]){return n;}
显然,括号在许多不同的情境下可能会改变意义或解释。有人能解释一下幕后到底发生了什么吗?编译器如何知道如何在每个情境中解释?是有一个通用指导方针还是每种情况都有具体规则?
我对编译器对括号的解释有点困惑。有人可以解释一下在这种情况下实际发生了什么吗?
类型转换:(int)a
或者 int(a)
参数传递:
template <typename t>
int size(t (&)[n]){return n;}
int(value)
(我强调)。所以这意味着一个简单类型说明符(7.1.5)后面跟着一个带括号的表达式列表,根据表达式列表构造指定类型的值。如果表达式列表只有一个表达式,类型转换表达式在定义性和含义上等同于相应的强制类型转换表达式(5.4)
int(value)
并且
(int)value
template <typename T, size_t N>
size_t (T (&)[N]) {
return N;
}
N
和T
都是模板参数,允许您传入任何数组,同时编译器会自动填充N
为数组的元素数量。如果这看起来有点困惑(T (&)[N]
是什么鬼?),那是因为这个函数接受的参数类型是T (&)[N]
。为了让这个参数更容易阅读,让我们给它起个名字,如下所示:template <typename T, size_t N>
size_t (T (&array)[N]) {
return N;
}
T (&array)[N]
array
的变量,它是一个指向由T
类型的N
个元素组成的数组的引用。你确实可以声明数组的引用,就像你可以声明数组的指针一样。在实践中,这并不常见,但在这个特定的模板惯用语中,这是一种让编译器为你推断数组大小的好方法,因为它试图将数组与模板参数匹配。T& array[N]
array
的变量,它是一个包含N
个对象的数组,每个对象都是T&
类型的”。然而,C++规范明确禁止引用数组,因此这是非法的。括号明确消除了歧义。这与函数指针类似-你可以这样写void (*functionPointer)()
替代
void *functionPointer()
*
表示functionPointer
是一个指针,而不是返回void *
的函数。vector<int> v();
vector<int> v(10);
vector<int>
并将10
作为构造函数参数传递的语句,因为它不可能被视为函数原型。 规范的§6.8和§8.2通过指出任何可以被视为声明的内容都将被视为声明,任何可以被视为函数原型的内容也将被视为函数原型来处理这些情况。T (&array)[N]
),括号的情况由不同的逻辑处理,因为在声明变量或定义需要显式括号的参数类型的上下文中,你的意图是明确的,因为从上下文中清楚地表明你正在命名一个类型以便声明一个变量。T(value)
和 (T)value
的转换是相同的。T (&array)[N]
中的括号是为了防止编译器将 &
绑定到 T
而不是预期的 array
。T(value)
和(T)value
的强制转换是相同的。”这并不是真的。后者可以执行reinterpret_cast,而前者则不能。例如,使用前者将void指针转换为特定指针是行不通的,并且会导致编译器错误。 - Jimmy T.强制类型转换 (int)a 或 int(a)
(int)a 是一种类型转换
int(a) 是构造一个 int 类型,将 a 传递给 int 构造函数
表达式按照运算符的优先级、元数以及运算符是右结合还是左结合进行求值。请阅读您的 C++ 教材中的运算符优先级表。
获取 c++decl 程序的副本;它可以读取 C++ 表达式并输出该表达式的英语解释。 或者阅读此解释。
从C++14附录A中,括号可能出现在语法中的完整列表如下:
§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 )
if-group
和 elif-group
的预处理器规则确实涉及到 constant-expression
。lparen
表示没有前导空格的 (
raw-string
的规则是在词法分析期间进行的,因此 (
和 )
不会成为标记。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 )