【问题标题】:The declarations in the link below don't need the storage-class-specifier `static`. Am I correct? [duplicate]下面链接中的声明不需要存储类说明符“静态”。我对么? [复制]
【发布时间】:2016-04-08 20:12:25
【问题描述】:

以下是thiscppreference 页面关于变量模板的摘录

在类范围内使用时,变量模板声明一个静态数据 成员模板。

using namespace std::literals;
struct matrix_constants
{
    template<class T>
    using pauli = hermitian_matrix<T, 2>; // alias template

    template<class T> // static data member template
    static constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } }; 

    template<class T>
    static constexpr pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };

   template<class T>
   static constexpr pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
};

C++ 标准 (N4140) 在第 14.1 (1.4) 段中说明如下:

类范围的变量模板是静态数据成员模板。

因此,在我看来,上面链接中的声明不需要存储类说明符static。我说的对吗?

P.S.:我刚刚开始对模板进行调查。

【问题讨论】:

  • 您确定要使用可变模板开始您的模板调查吗?可能是更直接的东西?
  • @SergeyA 我知道标准不是学习 C++ 的最佳场所,但现在,这就是我正在做的事情,即我正在遵循标准。
  • @Barry 但是C++0z 仍然没有更正上面显示的示例。
  • @JohnKalane 是的,不幸的是该示例仍然不正确(请参阅 Richard Smith 在链接问题中的评论)

标签: c++ templates static c++14


【解决方案1】:

虽然我仍然认为您没有选择正确的课程来学习模板,但我将直接回答问题。

Standard(和 cppreference)的措辞意味着当模板变量是类成员时,它们应该被定义为static。这是对开发者的要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多