The typename keyword and nested qualifier

struct A{};

template <typename T>
struct B
{
    typename ::A a1; //(1)
    typename A a2; //(2): error
};

int main(){return 0;}

Why is the first case correct and the second not? I do not understand the meaning of this restriction.
And anyway, why is the first case allowed? ::Anot dependent on the parameter template. What is the point of it?

+4
source share
2 answers

As @MikeSeymour answer is explained, strictly following the standard (C ++ 11, I do not have text in C ++ 14), case (1) should also be wrong - typenamethe qualified name prefix can only be used when ::there is at least one name.

, @hvd, CWG issue 382 , , typename , , . , , -, , .

, (2) , (1) . (, , ) , " , , , typename". "typename, , , , ". (1)

, , , typename typename, .


(1) , . , , - (-, ), typename-specifier ( typename). ( 14.6) , typename , . typename , , ( ).

+5

, typename, . :

  • typename,
  • typename, .

id,

typename-specifier:
    typename nested-name-specifier identifier
    typename nested-name-specifier template<opt> simple-template-id

nested-name-specifier:
    :: (C++14 or later)
    ::<opt> type-name ::
    ::<opt> namespace-name ::
    decltype-specifier ::
    nested-name-specifier identifier ::
    nested-name-specifier template<opt> simple-template-id ::

, , , , . , ++ 14 , :: .

+6

Source: https://habr.com/ru/post/1570152/


All Articles