C++ [RFC] taking address of a static const data member

Fariborz Jahanian fjahanian@apple.com
Fri Mar 11 19:15:00 GMT 2005


Section 9.4.2 of c++ standard "Static data members"  does not directly 
address this issue. But there is
a dejagnu c++ test case which explicitly disallows (by issuing a 
link-time error) taking address of a static
const data member.  Test case is const2.C.
This question has come up because, g++-4.0 (ppc-darwin target) issues 
the
same link error for the following test case (which requires taking 
address of Foo::foo).

#include <map>
struct Foo { static const int foo = 0x3ab; };
int main()
{
         std::map<int, int> m;
         m[Foo::foo];
}

And here is const2.C for easy reference:

/ { dg-do link }
// This test should get a linker error for the reference to A<int>::i.
// { dg-error "i" "" { target *-*-* } 0 }

template <class T> struct B { static const int i = 3; };
template <class T> struct A { static const int i = B<T>::i; };
const int *p = &A<int>::i;

int main(){}

So, is g++ correct in rejecting this seemingly good user code?

- Thanks, fariborz (fjahanian@apple.com)



More information about the Gcc mailing list