Example: template <typename T> T const max1(T const a, T const b) { return a < b ? b : a; } template <typename T> T const & max2(T const & a, T const & b) { return a < b ? b : a; } struct Test { static int const A = 1; }; int main() { #if 0 max1(Test::A, 2); // OK. #else max2(Test::A, 2); // Does not link: undefined reference to `Test::A #endif } The problem does not appear in 2.95.4 The problem does appear in 3.0.4, 3.2.3 Also appeared on 3.4.4 x86_64
You need to declare space for the variable still like so: int const Test::A; This is invalid.
You are correct. I was wrong. I thought, because of consistency reasons, that 1) both should link, or 2) both shouldn't link. (And I choose 1) for the "bug"-report.) As I now understand it, the "static int const A = 1;" will be seen as an "integral constant expression". If, OTOH, a reference has to be taken, it is seen as a "normal int", thus requiring an actual definition. (It is in 9.4.2 of the standard.) See also bugs 14404, 13259, 15244, 17673. (I search the bugs database before filing, but could not find it.) It stills /looks/ inconsistent though.
Subject: Re: No proper linkage created for in class initialized static const On Mar 15, 2005, at 1:46 PM, dopheide at fmf dot nl wrote: > It stills /looks/ inconsistent though. That is because there is an optimization going on here which is allowed by the standard. -- Pinski