Variable template at class scope

后端 未结 2 347
小鲜肉
小鲜肉 2020-12-07 04:54

Using N3651 as a basis,

A variable template at class scope is a static data member template.

The example given is:

2条回答
  •  星月不相逢
    2020-12-07 05:26

    EDIT: 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.


    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
       using pauli = hermitian_matrix;
      template
       constexpr pauli sigma1 = { { 0, 1 }, { 1, 0 } };
      template
       constexpr pauli sigma2 = { { 0, -1i }, { 1i, 0 } };
      template
       constexpr pauli sigma3 = { { 1, 0 }, { -1, 0 } };
    };
    

    The example in 14.5.1.3 Static data members of class templates [temp.static]/1 notably does use static:

    struct limits {
      template
        static const T min; // declaration
    };
    
    template
      const T limits::min = { }; // definition
    

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

    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.

提交回复
热议问题