为什么模板别名专用化取决于引用它的上下文
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++标准中,在哪里解释模板参数被模板参数替换是在引用别名专用化的上下文中执行的?
无处可去。这是核心问题 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设置一个实例。但是在第二种情况下,您直接尝试并且不起作用,
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- #为""定义宏;静态";针对不同的上下文
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 与互斥锁相比,旋转锁可以保证上下文切换
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 为什么模板别名专用化取决于引用它的上下文
- c++ lambda通过引用和' this '指针捕获上下文
- QML 报告引用错误:XYZ 未在添加到上下文C++对象上定义