[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 14:12:24 PST 2014


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

> On 11/15/2014 09:51 AM, Gabriel Dos Reis wrote:
> > 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.
>
> That's what I was hoping.
>
> Maybe the problem is I'm not making my point clear enough.
> If I understand your proposal, then the following code should
> compile without error when:
>   !defined(USE_STATIC) && !defined(DUPLICATE_DECLARATION)
> yet, with clang 3.5, it only compiles with both these
> macros are define.
>
> Is clang wrong?
>

You still have variable template as class member in your original example,
so you have to  obey the rules of class members...

Note that your original example was (also) missing the keyword "static" --
the C++ object model does not permit non-static data member templates.

-- Gaby


>
> >
> > -- Gaby
> >
> >
> >
> > _______________________________________________
> > cfe-users mailing list
> > cfe-users at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users
> >
>
>
> _______________________________________________
> 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/20141115/b308f4bf/attachment.html>


More information about the cfe-users mailing list