为什么模板别名专用化取决于引用它的上下文

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

本文关键字:引用 上下文 取决于 别名 专用 为什么      更新时间:2023-10-16

请考虑以下示例代码:

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>::ptpt_type<V<int>> 的别名。然而,它的定义取决于它所引用的上下文。

在C++标准中,在哪里解释模板参数被模板参数替换是在引用别名专用化的上下文中执行的?

无处可去。这是核心问题 1554。

从 14.5.7 [temp.alias] 的当前措辞中不清楚别名模板和访问控制的交互。例如:

template <class T> using foo = typename T::foo;
class B {
  typedef int foo;
  friend struct C;
};
struct C {
  foo<B> f;    // Well-formed?
};
using pt_type = typename T::type;无法访问

V::type,因为类型是私有的。

以下作品:

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
}

在 V::p t 中,您正在访问您的"自己的"类型,您可以这样做,但在第二种情况下,私有使它变得不可能。所以 V::p t 在传递你的私有类型 int 时pt_type设置一个实例。但是在第二种情况下,您直接尝试并且不起作用,