我对编译器对括号的解释有点困惑。有人可以解释在这种情况下实际发生的事情吗?
铸造:(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)
这就是所谓的显式类型转换,由 §5.2.3 管理。确切的措辞是这样说的
一个简单类型说明符(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]
什么?),那是因为这个函数接受了一个 type 的参数T (&)[N]
。为了使这更容易阅读,让我们给这个参数一个名字,如下所示:
template <typename T, size_t N>
size_t (T (&array)[N]) {
return N;
}
我认为这使这更容易阅读。但是这个声明是什么意思呢?
T (&array)[N]
这声明了一个名为的变量,它是对包含s 个元素array
的数组的引用。您确实可以声明对数组的引用,就像您可以声明指向数组的指针一样。这在实践中并不常见,但在这个特定的模板习语中,它是一种让编译器为您推断数组大小的好方法,因为它试图将数组与模板参数匹配。T
N
在这种情况下括号的原因是如果你写
T& array[N]
编译器会将其解析为“一个名为的变量,它是一个对象array
数组N
,每个对象都是一个T&
. 但是,C++ 规范明确禁止引用数组,这是非法的。括号明确地消除了歧义。这类似于函数指针 - 你写
void (*functionPointer)()
代替
void *functionPointer()
为了让编译器意识到它*
是functionPointer
一个指针,而不是一个返回的函数void *
。
至于编译器如何确定何时以每种方式处理括号,规则相当复杂,实际上在某些情况下编译器不会以预期的方式解析您的表达式。其中一种情况是通俗地称为“最令人烦恼的解析”,其中编译器将看起来像对象构造的东西视为函数原型。例如,这段代码:
vector<int> v();
不使用默认构造函数创建vector<int>
被调用v
初始化。相反,它将 this 视为被调用函数的函数原型,该函数v
不接受任何参数并生成vector<int>
! 但是,如果你要写
vector<int> v(10);
然后编译器可以明确地推断这是作为构造函数参数vector<int>
传递的声明10
,因为它不可能被视为函数原型。规范的 §6.8 和 §8.2 处理这些情况,说任何可以被视为声明的东西都将是,任何可以被视为函数原型的东西也将是。
数组上下文中的括号大小写(即T (&array)[N]
)由不同的逻辑处理,因为在声明变量或定义类型需要显式括号的参数的上下文中,不会有歧义关于您的意图,因为从上下文中可以清楚地看出您正在命名一个类型以声明一个变量。
总结一下——
T(value)
和(T)value
是相同的。T (&array)[N]
是为了防止编译器按预期绑定&
toT
而不是 to array
。希望这可以帮助!
铸造 (int)a 或 int(a)
(int)a 是一个演员表
int(a) 是 int 的构造,将 a 传递给 int ctor
根据运算符的优先级、数量以及运算符是右结合还是左结合来评估表达式。阅读 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
和do的预处理器规则elif-group
参考constant-expression
.lparen
表示 a(
前面没有空格raw-string
是在词法分析期间,因此(
and)
不会成为标记。在您的问题中,您使用以下内容:
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 )