根据C++17标准,[temp.point]/4,强调我的,
For a class template specialization, a class member template specialization, or a specialization for a class member of a class template, if the specialization is implicitly instantiated because it is referenced from within another template specialization, if the context from which the specialization is referenced depends on a template parameter, and if the specialization is not instantiated previous to the instantiation of the enclosing template, the point of instantiation is immediately before the point of instantiation of the enclosing template. Otherwise, the point of instantiation for such a specialization immediately precedes the namespace scope declaration or definition that refers to the specialization.
我不明白这句话怎么解释。在其他地方,当描述模板中的名称查找时,标准指的是“实例化上下文”和“定义上下文”。在那里,“上下文”一词似乎意味着源文本中的一个特定点,它决定了可见名称的集合。在这里,我不确定“上下文”的含义是否相同。如果这就是它的意思,我不确定它依赖于模板参数意味着什么。这是否意味着它在模板内部,或者它是否特别意味着在编译器决定实例化相关特化或其他情况时,封闭模板中仍有一些未绑定(bind)的模板参数?
示例将不胜感激。
最佳答案
此上下文出现在封闭模板内(来自另一个模板特化 并且取决于模板参数)。它必须依赖于封闭模板的模板参数,因此它可能是封闭模板定义上下文中的上下文。
所以我想这个段落可以用这个例子来说明:
template<class T> struct A{};
template<class T> struct B{};
//point of instantiation of B<int>
template<class T> void f(){
A<T> a; //The context depends on enclosing template parameter T
B<int> b; //The context does not depend on a template parameter.
}
//
void g(){
f<double>();
}
//point of instantiation of A<double>
//point of instantiation of f<double>
关于c++ - "if the context from which the specialization is referenced depends on a template parameter"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56812462/