parsing - Different meanings of parentheses in C++? -


i bit confused withnthe interpretation of parentheses compiler. can 1 please explain happens in such contexts?

casting: (int)a or int(a)

parameter passing:

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

obviously there many different contexts parentheses change meaning or interpretation. can 1 please explain exaactly happening behind curtain? how compiler know how interpret in each context? there general guideline or specific rule each case?

thanks

captain pedantic rescue!

if write

int(value) 

this what's known explicit type conversion , governed §5.2.3. exact wording says that

a simple-type-specifier (7.1.5) followed parenthesized expression-list constructs value of specified type given expression list. if expression list single expression, type conversion expression equivalent (in definedness, , if defined in meaning) corresponding cast expression (5.4)

(my emphasis). means that

int(value) 

and

(int)value 

are identical 1 another. it's pick whichever of these find easier write.

as second question, in example gave templates , array, believe meant write this.

template <typename t, size_t n> size_t (t (&)[n]) {     return n; } 

here, n t template parameter, allows pass in array you'd while having compiler fill in n number of elements in array. in case looks confusing (what on earth t (&)[n]?), it's because function taking in parameter of type t (&)[n]. make bit easier read, let's give parameter name, shown here:

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

i think makes bit easier read. declaration mean?

t (&array)[n] 

this declares variable called array reference array of ts of n elements. can indeed declare references arrays, can declare pointers arrays. not common in practice, in particular template idiom great way of having compiler infer size of array tries match array template argument.

the reason parentheses in case if write

t& array[n] 

the compiler parse "a variable called array that's array of n objects, each of t&. however, c++ spec disallows arrays of references, , illegal. parentheses explicitly disambiguate this. similar function pointers - write

void (*functionpointer)() 

instead of

void *functionpointer() 

to make compiler realize * means functionpointer pointer, rather function returns void *.

as how compiler determines when treat parentheses in each way, rules complex , there few circumstances in compiler not parse expression in intended way. 1 of these cases colloquially referred "the vexing parse" in compiler treats looks object construction function prototype. example, code:

vector<int> v(); 

does not create vector<int> called v initialized using default constructor. instead, treats function prototype function called v takes no arguments , produces vector<int>! however, if write

vector<int> v(10); 

then compiler can unambiguously infer declaration of vector<int> passing 10 constructor argument, because there's no way treated function prototype. §6.8 , §8.2 of spec handles these cases saying can treated declaration be, , can treated function prototype well.

the case of parentheses in context of array (that is, t (&array)[n]) handled different piece of logic because in context in you're declaring variable or defining parameter type requires explicit parenthesis, there can no ambiguity intention because it's clear context you're naming type in order declare variable.

to summarize -

  1. casts of form t(value) , (t)value identical.
  2. the parentheses in t (&array)[n] prevent compiler binding & t instead of array intended.
  3. the particular use of parenthesis inferred context, though issues can come between variable declarations , function prototypes.

hope helps!


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -