[cfe-users] variable template within class produces non-static data member cannot be constexpr

David Blaikie dblaikie at gmail.com
Fri Nov 14 11:01:48 PST 2014


On Fri, Nov 14, 2014 at 4:23 AM, Larry Evans <cppljevans at suddenlink.net>
wrote:

> On 11/14/2014 12:48 AM, David Blaikie wrote:
> > My guess is that the proposal was written assuming a certain
> implementation
> > of constexpr that never panned out.
> >
> > It looks like both GCC and Clang expect constexpr member variables to be
> > explicitly marked static:
> >
> > const.cpp:5:19: error: non-static data member 'f' declared 'constexpr'
> >    constexpr foo f{};
> >                    ^
> >
> > (is GCC 4.9's diagnostic - for a simple non-template constexpr member
> variable)
> >
> That's sad because, as n3651 says on pp. 2-3:
>
>   The main problems with “static data member” are:
>
>   • they require “duplicate” declarations: once inside the class
>     template, once outside the class template to provide the “real”
>     definition in case the constants is odr-used.
>
>   • programmers are both miffed and confused by the necessity of
>     providing twice the same declaration. By contrast, “ordinary”
>     constant declarations do not need duplicate declarations.
>

I don't think this is referring to the need to write "static" in a static
member variable template declaration.

This is referring to the workaround (In the absence of static variables) of
using a class template with a nested static data member - pointing out that
when you write it that way (see the example near this workaround
description) you have to write the class, the variable declaration, and the
variable definition. Whereas with a plain non-member variable template you
don't need separate declaration/definition - the variable template (like a
function template) is the declaration+definition all in one.


> Also, the clang status page:
>
>   http://clang.llvm.org/cxx_status.html
>
> reference n3651 below the section title:
>
>   C++14 implementation status
>
> Hence, I thought that's what clang implemented.
>

Ah, here's the wording that's important:

"A variable template at class scope is a static data member template." in
14 paragraph 1.

(sorry, I'd been looking in the static member parts of the spec, not the
variable template parts, which have this special case)

It looks like this just hasn't been implemented - Larisse (CC'd) did the
initial variable template implementation. Perhaps she can tell us if that
was a more recent change, whether it's planned to be fixed, or a bug in the
spec etc.


> In addition, the latest standard *draft*:
>
>   https://isocpp.org/files/papers/N3797.pdf
>
> contains the n3651 matrix_constants example on p. 313.
>
> Is there some more current standards document which requires the
> static qualifier on variable templates within a class?


> TIA.
>
> -regards,
> Larry
>
> > On Thu, Nov 13, 2014 at 8:38 PM, Larry Evans <cppljevans at suddenlink.net>
> > wrote:
> >
> >> The following code:
> >>
> >> //{=======================================
> >> #include <type_traits>
> >> struct variable_template
> >> {
> >>     template <int Value>
> >>     std::integral_constant<int,Value> constexpr
> >>   value{}
> >>     ;
> >> };
> >> #include <iostream>
> >> #include <iomanip>
> >> int main()
> >> {
> >>     std::cout<<
> >>       variable_template::
> >>       value<1>()<<std::endl;
> >>     return 0;
> >> }
> >> //}=======================================
> [snip]
>
>
> _______________________________________________
> cfe-users mailing list
> cfe-users at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20141114/c1bea49b/attachment.html>


More information about the cfe-users mailing list