gpt4 book ai didi

c++ - 静态数据成员模板特化的实例化点在哪里

转载 作者:行者123 更新时间:2023-12-01 12:34:41 24 4
gpt4 key购买 nike

考虑以下代码:

#include <iostream>
template<typename T>
struct Test{
template<typename U>
static U value;
};
template<typename T>
template<typename U>
U Test<T>::value = U{};
//#1
int main(){
auto d = Test<int>::value<int>;
}
//#2
标准中的 [temp.point] 部分涵盖了实例化点应放置的大多数情况。但是我认为静态数据成员模板还不清楚,因为:
temp.point#1

For a function template specialization, a member function template specialization, or a specialization for a member function or static data member of a class template, if the specialization is implicitly instantiated because it is referenced from within another template specialization and the context from which it is referenced depends on a template parameter, the point of instantiation of the specialization is the point of instantiation of the enclosing specialization. Otherwise, the point of instantiation for such a specialization immediately follows the namespace scope declaration or definition that refers to the specialization.


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.


两段都分别涵盖了他们提到的案例,它们是 a specialization for static data member of a class templatea class member template specialization ,所以,专精为 静态数据成员模板可以叫 a specialization for static data member of a class templatea class member template specialization ?我更愿意将其视为 类(class)成员模板特化 , 我的理由在第一段,它提到了 成员函数模板特化 ,这意味着如果 A 是 X 的特化模板,它会称之为 X模板特化,但这只是我的推断。
在 [temp.static] 部分,它意味着 静态数据成员和静态数据成员模板 统称为类的静态数据成员或类模板。
temp.static#1

A definition for a static data member or static data member template may be provided in a namespace scope enclosing the definition of the static member's class template.


[Note: A specialization of a static data member template is a static data member. A specialization of a member function template is a member function. A specialization of a member class template is a nested class.  — end note]


现在,措辞使问题更加不清楚。所以根据上面的规则,是 Test<int>::value<int>的实例化点在 #2#1 ?
如果 Test<int>::value<int>的POI在 #2 ,那么它将被视为 a specialization for static data member of a class template , 否则如果它在 #1 ,那么它将被视为 a class member template specialization ,我不知道哪个位置是正确的。如果我错过了什么,请纠正我。

最佳答案

您可能会混淆实例化/特化

template<typename T>
template<typename U>
U Test< T >::value = 88; // <- this is not specialization

template<>
template<>
int Test< int >::value<int> = 98; // <- this is specialization
运行此代码 https://godbolt.org/z/h434eG ,查看输出中数字的顺序,然后取消对该块的特化注释并再次运行。

关于c++ - 静态数据成员模板特化的实例化点在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62768992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com