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

Larry Evans cppljevans at suddenlink.net
Wed Nov 19 08:32:02 PST 2014

On 11/14/2014 01:01 PM, David Blaikie wrote:> 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
> 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.

So, if the keyword, static, *is* used, a separate definition *outside* of
the class (IOW, in, I think the terminology is, "in namespace scope")
is needed. In contrast, if keyword static *is not* used, the
separate definition is not required ( even though the
variable is still a static member) ?

>> 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.
The reponse to my comment here:



  the examples in the standard have an error, mentioned

where _here_ links to:


which is, essentially, the question posed in my OP to this
thread.  In addition, the comments to that SO post contain:

  _The committee is aware that the wording for variable
  templates needs some help in various places_

which links to:


which I'm still trying to digest :(, but it seems to
indicate the variable template meaning is ambiguous; hence,
one can't say clang has a bug in its implementation
of variable templates.


More information about the cfe-users mailing list