Why a template alias specialization depends on the context in which it is referred?

北慕城南 提交于 2021-02-04 19:00:09

问题


Consider this example code:

template <class T>
using pt_type = typename T::type;

template <class T>
class V {
  using type = int;
  public:
  using pt = pt_type<V>;
};

void g() {
  V<int>::pt a; // Does compile
  pt_type<V<int>> b; // Does not compile
}

V<int>::pt is an alias for pt_type<V<int>>. Nevertheless the fact it is defined depends on the context where it is referred.

Where is it explained in the C++ standard that the substitution of the template parameter by the template argument is performed in the context where is refered the alias specialization?


回答1:


Nowhere. This is core issue 1554.

The interaction of alias templates and access control is not clear from the current wording of 14.5.7 [temp.alias]. For example:

template <class T> using foo = typename T::foo;

class B {
  typedef int foo;
  friend struct C;
};

struct C {
  foo<B> f;    // Well-formed?
};



回答2:


using pt_type = typename T::type; can't access V::type becasue type is private.

The following works:

template <class T>
using pt_type = typename T::type;

template<class T>
class V
{
  public:
    using type = int;
    using pt = pt_type<V>;
};

void g()
{
    V<int>::pt a; //Do compile
    pt_type<V<int>> b; //Do not compile
}



回答3:


In V::pt you are accessing to your "own" type and you can do it, but the private makes it impossible in the second case. So V::pt crates an instanciation of pt_type passing your private type int. But in the second case you try directly and it does not work,



来源:https://stackoverflow.com/questions/48453343/why-a-template-alias-specialization-depends-on-the-context-in-which-it-is-referr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!