This behaviour is vexing me time and again. The cause of the trouble is that your
A(int n): v(n, A::kDefaultValue) {}
odr-uses the static constexpr
member, since the constructor of v
takes a constant reference second argument. Odr-usage requires a definition somewhere, i.e.
const int A::kDefaultValue;
in some compilation unit (which is compiled and linked to main()
). This requirement has been dropped in C++17 and the corresponding definition (as above) deprecated.
However, a definition is not always possible (for example for members of class templates) and the simplest way to avoid both the definition and your error is
A(int n): v(n, int(A::kDefaultValue)) {}
which creates a temporary to be passed to the constructor of v
(but since the latter is fully inline, the compiler may optimise that away).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…