Consider the following C++:
struct proxy {
int operator=(int x) {
return x;
}
};
namespace foo {
template<typename T>
inline int bar = 0;
}
template<>
inline int foo::bar<int> = proxy{} = 42;
int main() {}
(Live example here: https://godbolt.org/z/j8Gqj8r39)
This code causes the following linker error on MSVC only (GCC and Clang are ok):
error LNK2001: unresolved external symbol "int bar<int>" (??$bar@H@@3HA)
Which is strange because there's only 1 translation unit, all the symbols are fully defined, and bar isn't even accessed.
The error seems to only occur if all of the following are true:
baris in a namespacebaris templated- The specialisation of
baris at global scope - The specialisation of
barisinline - The specialisation of
baris initialised viaproxy
Given the bizarre circumstances I can only think that it's undefined behaviour (no idea how that could occur though) or a bug with the MSVC compiler/linker. What's going on here?
P.S. please don't ask why I need to write strange code like this...