Variable template at class scope

后端 未结 2 345
小鲜肉
小鲜肉 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:21

    If you try the first example, clang coughs up the magic error right away:

    template 
    struct hermitian_matrix { };
    
    struct foo
    {
        template 
        using pauli = hermitian_matrix;
    
        template 
        constexpr pauli sigma1 = { { 0, 1 }, { 1, 0 } };
    };
    
    error: non-static data member cannot be constexpr; did you intend to make it static?
    

    Apparently at class scope, variable templates need to be declared static. clang doesn't cough up the right error unless you declare it constexpr, which can be misleading. As well as that, their example of a static data member:

    struct foo
    {
        template 
        static T bar;
    };
    
    template 
    T foo::bar = T{3.14};
    

    may throw you off because one might think that the whole point of variable member templates is to replace static data members.

提交回复
热议问题