0

假设我们S在头文件中定义了一个包含 2 个成员(1 个 int 和 1 个 functor)的简单结构。我们在模板函数中创建一个这样的结构作为静态 const 变量,foo并返回这个结构的 const 引用。在c ++ 17中合法吗?我尝试调用foo<int>2 个翻译单元,它适用于 clang/gcc,但适用于 Visual Studio。使用 Visual Studio,第二个调用者返回错误m_n(0 而不是 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;
}
4

0 回答 0