0

Let's say we define a simple struct S containing 2 members (1 int and 1 functor) in a header file. And we create a such struct as a static const variable in a template function foo and return the const reference of this struct. Is it legal in c++17? I try to call foo<int> in 2 translation units and it works with clang/gcc but fails with Visual Studio. With Visual Studio the second caller returns a wrong m_n(0 instead of 5).

#include <iostream>

template<typename Func>
struct S {
    int m_n;
    Func m_func;
    S(int n, Func func): m_n(n), m_func(func) {}
};

template<typename T>
auto const& foo() noexcept {
    static auto const s=S(5, [](auto const&) noexcept { std::cout<<"lambda"<<std::endl; });
    return s;
}
wanghan02
  • 1,227
  • 7
  • 14

0 Answers0