Static constexpr initialized in-class: why do these examples behave differently?

Viewed 63

I tried to compile the following code, but failed.

#include <iostream>
#include <vector>

struct Foo {
  static constexpr int A = 10;
  static constexpr int B = 10;

  std::vector<int> dat;

  Foo() : dat(A, B) {}
};

int main() {
  Foo foo;
}

Error message:

$ g++ a.cpp -std=c++11
/tmp/ccSaJJrd.o: In function `Foo::Foo()':
a.cpp:(.text._ZN3FooC2Ev[_ZN3FooC5Ev]+0x25): undefined reference to `Foo::B'
collect2: error: ld returned 1 exit status

Curiously, the following code can be compiled:

#include <iostream>
#include <vector>

struct Foo {
  static constexpr int A = 10;

  std::vector<int> dat;

  Foo() : dat(A, 10) {}
};

int main() {
  Foo foo;
}

Why cannot the second argument receive a constexpr value?

0 Answers
Related