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

Gabriel Dos Reis gdr at integrable-solutions.net
Sat Nov 15 07:51:10 PST 2014


On Sat, Nov 15, 2014 at 6:33 AM, Larry Evans <cppljevans at suddenlink.net>
wrote:

> On 11/15/2014 06:59 AM, Gabriel Dos Reis 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.
> >>
> >
> > then don't put it in a class.
> >
>
> But then I don't understand why the problem was mentioned in n3651 on
> pp. 2-3.  I thought one of the reasons for the proposal was to free
> programmers from the need for "duplicate" declarations; however, the
> solution:
>
>   don't put it in a class
>
> seems to indicate that the problem can be avoided simply by not
> creating the problem.  IOW, the "duplicate" declaration in the
> archetypical example on p. 2, the numeric_limits example, can be
> avoided by not putting it in a template class (or struct in this
> case).
>
> In that case, I don't understand the rationale for proposal.
>
> I must be missing something :(
>
> -regards,
> Larry
>

If you hate (as I do) having to put a variable in a class template just so
that you can abstract over its type but then you are forced to provide a
"duplicate" declaration (the real definition) outside the class, then
variable templates remove that pain: they do what they were designed for.

If you want to change the language rules for not having to "redeclare" a
static data member at namespace scope, then that is a *different problem*;
one you should write a proposal for if you feel strongly about it.

-- Gaby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20141115/ba0610c5/attachment.html>


More information about the cfe-users mailing list