This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/44225] New: Workaround for error: ... cannot appear in a constant-expression


I observed the following issue with g++-4.3.4:

I can work around the restriction of initializing a static const integral 
class member with a non-constant expression.
This violates the standard and should be rejected by "-pedantic".
Correct me, if I'm wrong.

I cut down my code to the following (concededly a bit contrived) demo 
scenario:

--- Example Code ---
template <class NT, int x>
struct X {
  static const NT VALUE;
};
template <class NT, int x>
const NT X<NT,x>::VALUE = static_cast<NT>(x);

struct FailingTruth {
  static const bool VALUE = X<double,1>::VALUE < X<double,1>::VALUE;
}; 

template <int x, int y>
struct Test {
  static const bool VALUE = X<double,x>::VALUE < X<double,y>::VALUE;
};

struct WorkingTruth {
  static const bool VALUE = Test<1,2>::VALUE;
};

template <bool B>
struct UseAsTemplateParamter {
  enum {
    VALUE = B
  };
};

UseAsTemplateParamter<WorkingTruth::VALUE> object;

--- End Example Code ---

The compiler correctly complains with
error: âX<double, 1>::VALUEâ cannot appear in a constant-expression
in "FailingTruth".

However, when I encasulate the  very same condition using the Test template 
class, the "WorkingTruth" code is accepted by g++. I could even use 
WorkingTruth::VALUE as a non-type template parameter (see last line).


Am I missing something here, or does that really violate the standard.

Regards Jan.

PS: Don't get me wrong, I like this "feature", it is really useful, when doing 
some meta-programming involving floating-point constants :-)


-- 
           Summary: Workaround for error: ... cannot appear in a constant-
                    expression
           Product: gcc
           Version: 4.3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tuschjan at yahoo dot de
 GCC build triplet: GNU/Linux x86
  GCC host triplet: GNU/Linux x86
GCC target triplet: GNU/Linux x86


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44225


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]