类范围的变量模板 [英] Variable template at class scope

查看:144
本文介绍了类范围的变量模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 N3651 作为基础,


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

给出的示例为:

struct matrix_constants {  
 template <typename T>   
  using pauli = hermitian_matrix<T, 2>;

但是所有以下定义都会产生错误:

Yet all of the following definitions give an error:

struct foo
{
    template <typename T>
    T pi = T{3.14};
};

template <typename T>
struct foo2
{
    template <typename U = T>
    U pi = U{3.14};
};

template <typename T>
struct foo3
{
    template <T>
    T pi = 42;
};

error: member 'pi' declared as a template

推荐答案

编辑:委员会说了,Clang是正确的,要求静态数据成员模板的 static 关键字。 14/1中给出的示例不是正确。希望工作草案的下一个修订版本将删除文本中的歧义。

The committee has spoken, Clang is correct to require the static keyword for static data member templates. The examples given in 14/1 are not correct. Hopefully, the next revision of the working draft will remove the ambiguity from the text.



这个似乎 Clang中的一个错误,但草案标准中的措辞不明确。我相信的意图是关键字 static 是隐式的。如果这不是意图,推测标准的措辞将更多地沿着类范围的变量模板 必须 静态数据成员模板。而不是类范围的变量模板 静态数据成员模板。 ( N3797 §14/ 1) §14/ 1中给出的(不可否认的非规范性)示例声明了三个类成员变量模板,没有使用 static 关键字:


This seems to be a bug in Clang, but the wording in the draft standard is ambiguous. I believe the intention is that the keyword static is implicit. If that was not the intention, presumably the standard wording would be more along the lines of "A variable template at class scope must be a static data member template." instead of "A variable template at class scope is a static data member template." (N3797 §14/1) The (admittedly non-normative) example given in §14/1 declares three class member variable templates, none with the static keyword:

struct matrix_constants {
  template<class T>
   using pauli = hermitian_matrix<T, 2>;
  template<class T>
   constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
  template<class T>
   constexpr pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };
  template<class T>
   constexpr pauli<T> sigma3 = { { 1, 0 }, { -1, 0 } };
};

中的示例14.5.1.3类模板的静态数据成员[temp.static] / 1 显示 使用 static

struct limits {
  template<class T>
    static const T min; // declaration
};

template<class T>
  const T limits::min = { }; // definition

所以至少不禁止这样做。

so at the very least it's not forbidden to do so.

由于 @ RichardSmith在他的评论,该部分的实际规范文本与该示例矛盾。他们将Clang写入标准的文本,因此该示例诊断为不良形式。 委员会知道变量模板的措辞需要一些帮助在各个地方,所以我相信在下一个草稿/ C ++ 14将有一些清理。

As @RichardSmith states in his comment, the actual normative text of the section contradicts the example. They write Clang to the text of the standard, so the example is diagnosed as ill-formed. The committee is aware that the wording for variable templates needs some help in various places, so I'm sure there will be some cleanup in the next draft / C++14.

这篇关于类范围的变量模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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