English 中文(简体)
C++中母体的不同含义?
原标题:Different meanings of parentheses in C++?

我与汇编者对母轮的解释相混淆。 有些人可以解释在这种情况下究竟发生了什么?

Casting:a or int(a)

参数通过:

template <typename t>
int size(t (&)[n]){return n;}

显然,在父母改变含义或解释时,可能有许多不同的情况。 有些人能否解释在幕后究竟发生了什么? 汇编者如何了解如何在每种情况下解释? 是否有一条一般性准则,或者它是否是每一案例的具体规则?

增 编

最佳回答

救援队长!

如果是,

int(value)

这是所谓的explicit打字的转换,受第5.2.3条管辖。 确切的措辞是:

简单类型的审查者(7.1.5)之后,根据表述清单,括号的表述清单构成特定类型的价值。 如果表述清单是单一表述,则转换表述的类型(定义,如果定义)与相应的表达方式相同(,5.4)

(My emphasis). So this means that

int(value)

以及

(int)value

两者完全相同。 不管你发现哪一部分人更容易书写。

As for your second question, in the example you gave with the templates 以及 array, I believe that what you meant to write was something like this.

template <typename T, size_t N>
size_t (T (&)[N]) {
    return N;
}

页: 1 以及<代码>T是一个模板参数,允许你在汇编者填入时,通过你喜欢的任何阵容,同时填写阵列中的内容数目。 如果出现这种混淆(地球上有T (&)[N]?),则是因为这一功能的参数为T (&)[N]。 为了便于阅读,请将这一参数命名为:

template <typename T, size_t N>
size_t (T (&array)[N]) {
    return N;
}

我认为,这使我们更容易阅读。 但这一声明意味着什么?

T (&array)[N]

兹宣布一个称为<条码>的变量。 这是指一系列<代码>。 Ts of originallyNs. 你的确可以宣布提及阵列,就像你可以宣布阵列点一样。 这种做法在实践中并不常见,但在这个特定模板中,由于汇编者试图将阵列与模板论点相匹配,因此是使汇编者对你阵列规模进行推断的重要途径。

本案中父母一方的理由是,如果你写了字的话。

T& array[N]

The compiler would parse this as "a variable called array that s an array of N objects, each of which is a T&. However, the C++ spec specifically disallows arrays of references, 以及 this would be illegal. The parentheses explicitly disambiguate this. This is similar to function pointers - you write

void (*functionPointer)()

instead of

void *functionPointer()

To make the compiler realize that the * means that functionPointer is a pointer, rather than a function that returns a void *.

As for how the compiler determines when to treat parentheses in each way, the rules are fairly complex 以及 there are actually a few circumstances in which the compiler will not parse your expression in the intended way. One of these cases is something colloquially referred to as "the most vexing parse" in which the compiler treats what looks like object construction as a function prototype. As an example, this code:

vector<int> v();

Does not create a vector<int> called v initialized using the default constructor. Instead, it treats this as a function prototype for a function called v that takes no arguments 以及 produces a vector<int>! However, if you were to write

vector<int> v(10);

Then the compiler can unambiguously infer that this is a declaration of a vector<int> passing 10 as a constructor argument, because there s no way that it could be treated as a function prototype. §6.8 以及 §8.2 of the spec h以及les these cases by saying that anything that can be treated as a declaration will be, 以及 anything that can be treated as a function prototype will be as well.

The case of parentheses in the context of the array (that is, T (&array)[N]) is h以及led by a different piece of logic because in the context in which you re declaring a variable or defining a parameter whose type requires explicit parenthesis, there can be no ambiguity about your intention because it s clear from context that you re naming a type in order to declare a variable.

To summarize -

  1. Casts of the form T(value) 以及 (T)value are identical.
  2. The parentheses in T (&array)[N] are to prevent the compiler from binding the & to T instead of to array as intended.
  3. The particular use of parenthesis is usually inferred from context, though some issues can come up between variable declarations 以及 function prototypes.

希望这一帮助!

问题回答

cast(int)a或(a)

(int)a

(a) 构筑一只 in子,穿过一只ctor子。

Expressions are evaluated according to operators precedence, arity, and whether the operator is right or left associative. Read the operator precedence chart in your C++ text.

查阅方案C++decl的复印件;读到C++的表述和产出,对表述的英文兰热解释

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 )

注:

  • 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.

阁下:

  • 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 )




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签