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

Larry Evans cppljevans at suddenlink.net
Mon Nov 24 09:01:00 PST 2014


On 11/19/2014 10:32 AM, Larry Evans wrote:
> 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
> 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.
>>
> 
> 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:
> 
>   http://en.cppreference.com/w/Talk:cpp/language/variable_template
> 
> says:
> 
>   the examples in the standard have an error, mentioned
>   _here_
> 
> where _here_ links to:
> 
> 
> http://stackoverflow.com/questions/21474111/variable-template-at-class-scope/21482264?noredirect=1#comment32627331_21482264
> 
> 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:
> 
> 
> https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/ZTw3W3vjWWs
> 
> 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.
> 
My last response to the talk:

http://en.cppreference.com/w/Talk:cpp/language/variable_template

contains:

  the definition outside the class definition is ...
  *only* optional if a reference is not made to the template variable.

and a reference to the standard's 9.4.2p3 is made to justify that
conclusion.

Hence, AFAICT, clang is correct in requiring the outside class defintion
when the template variable is referenced.

-regards,
Larry









More information about the cfe-users mailing list