[cfe-dev] In-class initialization doesn't work for template

Jens Kilian jjk at gondolin.bb.bawue.de
Sat Jul 7 06:05:57 PDT 2012

For the following program, clang (see below for version) complains about the in-class initialization of a struct using a constant pointer, but only when it occurs in a template - the non-template case works:

struct foo
  int *pi;

struct fie
  static int i;

struct bar
  static constexpr foo f = { &fie::i };  // works

template <class T>
struct baz
  static constexpr foo f = { &T::i };    // error

class zip
  const foo *p, *q;

zip::zip(void) : p(&bar::f), q(&baz<fie>::f) {}

jjk$ clang++ --version
Apple clang version 3.1 (tags/Apple/clang-318.0.54) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.0
Thread model: posix
jjk$ clang++ --std=c++11 --stdlib=libc++ -O3 -S foo.cpp 
foo.cpp:19:24: error: in-class initializer for static data member of type 'const foo' requires 'constexpr' specifier
  static constexpr foo f = { &T::i };
                       ^   ~~~~~~~~~
foo.cpp:29:33: note: in instantiation of template class 'baz<fie>' requested here
zip::zip(void) : p(&bar::f), q(&baz<fie>::f) {}
1 error generated.

Is this a bug in clang, or am I doing something illegal?


mailto:jjk at acm.org         phone:+49-7031-4357-122
http://www.bawue.de/~jjk/  fax:+49-7031-4357-483
http://del.icio.us/jjk     As the air to a bird, or the sea to a fish,
                           so is contempt to the contemptible. [Blake]

More information about the cfe-dev mailing list