Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"expected nested-name-specifier before ‘const’ error" with typename const in g++

Tags:

c++

g++

I have this code in C++

template<typename T>
class DD
: public enumerables<T>

{
...
private:
    typename const DD<T>& mContainer;
}

And it gives me two error messages :

  1. error: expected nested-name-specifier before ‘const’
  2. error: invalid declarator before ‘&’ token

What's wrong with typename const code? It compiles fine with MSVC C++.

ADDED

typename DD<T>& const mContainer; and const typename DD<T>& mContainer; give me the same error.

like image 822
prosseek Avatar asked Mar 09 '11 17:03

prosseek


2 Answers

Well, what's that typename doing there? You are not referring to a nested type, so typename is totally unnecessary there. I'd say that the error is caused by that unjustified use of typename, not by ordering of the parts of the declaration or anything else.

It should be just

const DD<T>& mContainer;

or even

const DD& mContainer;
like image 138
AnT Avatar answered Sep 26 '22 00:09

AnT


Except when introducing a template type parameter, the keyword typename must always be immediately followed by an optional global-scope :: token and then a nested-name-specifier; that is, something which has one or more namespaces or classes, each followed by the :: token.

See the syntax rules in the C++ Standard: 5.2 (function-style cast), 7.1.5.3 (elaborated type specifier), and 7.3.3 (using declaration).

Also, 14.6p5: "The keyword typename shall be applied only to qualified names, but those names need not be dependent."

Microsoft's compiler is wrong to accept the invalid syntax.

like image 37
aschepler Avatar answered Sep 24 '22 00:09

aschepler