为什么模板别名专业化取决于引用它的上下文?
Why a template alias specialization depends on the context in which it is referred?
考虑这个示例代码:
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
是 pt_type<V<int>>
的别名。然而,它被定义的事实取决于它被引用的上下文。
在 C++ 标准 中哪里解释了用模板参数替换模板参数是在引用别名特化的上下文中执行的?
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::pt 中,您正在访问您的 "own" 类型,您可以这样做,但在第二种情况下,私有使得它不可能。
所以 V::pt 创建了一个 pt_type 的实例,传递了你的私有类型 int。
但是第二种情况你直接试了不行,
无处可去。这是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?
};
考虑这个示例代码:
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
是 pt_type<V<int>>
的别名。然而,它被定义的事实取决于它被引用的上下文。
在 C++ 标准 中哪里解释了用模板参数替换模板参数是在引用别名特化的上下文中执行的?
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::pt 中,您正在访问您的 "own" 类型,您可以这样做,但在第二种情况下,私有使得它不可能。 所以 V::pt 创建了一个 pt_type 的实例,传递了你的私有类型 int。 但是第二种情况你直接试了不行,
无处可去。这是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? };