On 11/15/2014 09:51 AM, Gabriel Dos Reis wrote: > On Sat, Nov 15, 2014 at 6:33 AM, Larry Evans <cppljev...@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 <cppljev...@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? > > -- Gaby > > > > _______________________________________________ > cfe-users mailing list > cfe-users@cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users >
//Purpose: // See if code using "variable templates" proposed here: /* http://isocpp.org/files/papers/N3651.pdf */ // will compile. In particular, the 'struct matrix_constants' // in the reference seems essentially the same as code below. //Result: // No. /* variable_templates.cpp:16:44: error: non-static data member cannot be constexpr; did you intend to make it static? std::integral_constant<Integral,Value> constexpr ^ static 1 error generated. */ //======================================= //#define DUPLICATE_DECLARATION //#define USE_STATIC #include <type_traits> struct matrix_constants { template<typename T> using pauli = std::integral_constant<T, 2>; template<typename T> #ifdef USE_STATIC static #endif pauli<T> constexpr sigma1 = {}; }; #ifdef DUPLICATE_DECLARATION template <typename T> matrix_constants:: pauli<T> constexpr matrix_constants:: sigma1; #endif #include <iostream> int main() { std::cout<<"sigma1="<<matrix_constants::sigma1<int>()<<"\n"; return 0; }
_______________________________________________ cfe-users mailing list cfe-users@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users