The following only links successfully with -std=c++17 or above. -std=c++14 or below gives an unresolved external error:
In function `C::f()':
C.cpp:(.text+0x19): undefined reference to `C::kConstant'
Why doesn't this work with the c++14 standard and does work with the c++17 standard? (tested with both GCC and Clang)
And why does the commented-out workaround work even on c++14?
C.h
#pragma once
class C {
public:
static constexpr int kConstant = 10;
int f();
};
C.cpp
#include "C.h"
#include <algorithm>
int C::f()
{
// This only works with -std=c++17 or above
return std::min (1, kConstant);
// This works even with -std=c++14
// return std::min (1, static_cast<int> (kConstant));
}
Main.cpp
#include "C.h"
int main()
{
C c;
c.f();
}
Update: This question isn't an exact duplicate, but has an answer which answers most of my question. It doesn't answer why the workaround works with -std=c++14, though, and the original doesn't. Is it perhaps because in the original the variable is odr-used, and in the workaround it is not?
-std=c++14)intprvalue. The const referencestd::minexpects thus doesn't try to bind tokConstant(and odr use it due to it). I edited in another duplicate that addresses it (which also has an answer mentioning a cast).-std=c++14due to trying to initialize the non-inline static constexpr member in its declaration, which should have no effect, rather than in its (missing) definition?