C ++ Syntax Syntax

Consider:

void f(std::pair<bool,bool> terms = std::pair<bool,bool>(1,1)) {} 

gcc 4.4 is ok, gcc 4.3 complains about error: expected ',' or '...' before '>' token . Correction:

 void f(std::pair<bool,bool> terms = (std::pair<bool,bool>(1,1))) {} 

What reason? Is this a bug in 4.3?

+6
c ++ gcc
source share
1 answer

This was a known issue. He believes the second comma separates parameter declarations. This is due to the fact that in the definition of the class the parameters of the default arguments are first only tokens, and then only later analyzed when the full body of the class is read. Since he thus does not parse the default argument, he does not notice that the comma is really a comma in the template argument list.

See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325 to read something. Quoted

Another problem is the collection of tokens that form the default argument expression. Default arguments containing template identifiers with more than one parameter are difficult to determine when the default argument ends. Consider

 template <int A, typename B> struct T { static int i;}; class C { int Foo (int i = T<1, int>::i); }; 

The default argument contains a comma with a comma. Does this comma need to be considered as part of the default argument expression, and not the beginning of another argument declaration? To accept this as part of the default argument, you will need to search by name T (to determine that "<" was part of the argument list of the template, and not less than the operator) until C is complete. Moreover, more pathological

 class D { int Foo (int i = T<1, int>::i); template <int A, typename B> struct T {static int i;}; }; 

it would be very difficult to accept. Although T is declared after Foo, T is in the default scope of Foo.

+8
source share

All Articles