I'm using C++17 and stumbled on a linker error with this kind of code
#include <memory>
struct SomeType
{
static const int MIN = 0;
static const int MAX = 0;
};
struct Range
{
Range(int min=0, int max=0) : min(min), max(max) {}
int min, max;
};
int main()
{
auto range = std::make_shared<Range>(
SomeType::MIN,
SomeType::MAX
);
return 0;
}
On linux using gcc 11.2 this causes linker errors but MSVC builds it without problems (Visual Studio 2019 Version 16.9.2).
undefined reference to `SomeType::MIN'
undefined reference to `SomeType::MAX'
but if I add inline before static const int MIN = 0; and static const int MAX = 0; then it works just fine.
Why does adding the inline fix the linking issue? Why is it a link time issue and not a compile time issue?