Is a local class dependent if it is declared in a function template?

Current C ++ compilers (the latest gcc, clang) require a keyword typenamein the example below:

template<class T>
struct A
{
};

template<class T>
void f(T)
{
    struct C
    {
    };
    typedef typename A<C>::Type Type; // typename required
}

If typenameomitted, gcc (4.9, 5.0) reports an error:

need 'typename' before 'A<f(T)::C>::Type' because 'A<f(T)::C>' is a dependent scope

This example is otherwise well-formed according to my reading of the C ++ 11 standard.

This behavior is apparently covered by the following wording:

[temp.dep.type] / 8

Type depends if it

  • template parameter

  • member of unknown specialization

  • a nested class or enumeration that is a member of the current instance,

  • cv-qual type, where the cv-unqualified type depends,

  • a composite type built from any dependent type,

  • , ,

  • -, , - , ,

  • decltype (), .

, [class.local], C , . , A<C> ?

, C :

template<typename T>
struct A
{
    typedef T Type;
};

template<class T>
void f(T)
{
    struct C
    {
        enum { value = T::value };
    };
    typedef typename A<C>::Type Type; // typename required
}

A<C> ?

+4
3

( ) C . A<C>::Type, typename .

: , . , f, C C, f. , ( [temp.expl.spec] /(1.6)):

template <typename T>
class A { class C{}; };

template <>
class A<int>::C { int i; };

:

,

  • , ,

, dyp example, C , T.
, , . -, T .

+4

, , . C f. , A<C> , . - , A<C>::Type . A<C>::Type . .

0

, , typename. , , , , GCC f<T>(T)::C ( ) T — , A<[f<T>(T)::]C>::Type.

1484 , , , , , , GCC typename .

0

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


All Articles