MSVC linker error with explicit specialisation of inline variable template in namespace initialised via proxy

Viewed 37

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:

  • bar is in a namespace
  • bar is templated
  • The specialisation of bar is at global scope
  • The specialisation of bar is inline
  • The specialisation of bar is initialised via proxy

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...

0 Answers
Related