静态数据成员模板专门化的实例化点在哪里 [英] where's the point of instantiation of static data member template specialization

查看:68
本文介绍了静态数据成员模板专门化的实例化点在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

#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]部分涵盖了实例化点所在位置的大多数情况。但是,由于以下原因,我认为尚不清楚静态数据成员模板:

The [temp.point] section in the standard covers the most case of where the point of instantiation shall place. However I think it's unclear about static data member template, due to:

温度点#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.

温度点#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是否是专业化的对于 X 模板,它将称为 X 模板专门化,但这只是我的推断。

Two paragraphs all respectively cover the case they mentioned, they are a specialization for static data member of a class template and a class member template specialization, So, the specialization for static data member template could be called a specialization for static data member of a class template or a class member template specialization? I prefer to consider it as a class member template specialization, My reason is in the first paragraph, it has mentioned a member function template specialization, that implies if A is a specialization for X template, it would call it a X template specialization, however It's just my inference.

在[temp.static]部分中,它表示静态数据成员和静态数据成员模板统称为类或类模板的静态数据成员。

In the section of [temp.static], it implies that a static data member and static data member template are collectively called static data member of class or class template.

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.



[注意:静态数据成员模板的特殊化是静态da ta成员。成员函数模板的特化是成员函数。成员类模板的一种特殊化是嵌套类。 —注释]

[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

Now, the wording makes the question more unclear. So according to the above rules, Is the point of instantiation for Test<int>::value<int> is at #2 or #1?

如果 Test< int> :: value< int>的POI ; 位于#2 ,则将其视为类模板的静态数据成员的专门化,否则,如果它位于#1 ,则它将被视为类成员模板专门化 ,我不知道哪个位置正确。如果我错过了某些事情,请纠正我。

If the POI of Test<int>::value<int> is at #2, then it will be considered as a specialization for static data member of a class template, otherwise if it's at #1, then it will be considered as a class member template specialization, I don't know which the position is correct. If I miss something, please correct me.

推荐答案

您可能会混淆实例化/专业化

You may be confusing instantiation/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 ,查看输出中数字的顺序,然后取消注释该块的特殊化并再次运行。

Run this code https://godbolt.org/z/h434eG, take a look at the order of the numbers on the output, then uncomment the block with a specialization and run again.

这篇关于静态数据成员模板专门化的实例化点在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆