代码之家  ›  专栏  ›  技术社区  ›  Mine

是否使用了静态constexpr变量odr?

  •  3
  • Mine  · 技术社区  · 7 年前

    Foo::FOO1 是否使用ODR?

    #include <iostream>
    #include <map>
    #include <string>
    
    class Foo
    {
    public:
        static constexpr auto FOO1 = "foo1";
        void bar();
    };
    
    void Foo::bar()
    {
        const std::map<std::string, int> m = {
            {FOO1, 1},
        };
        for (auto i : m)
        {
            std::cout << i.first << " " << i.second << std::endl;
        }
    }
    
    int main()
    {
        Foo f;
        f.bar();
        return 0;
    }
    

    -O1 -O0 ,我得到下面的错误(参见 coliru example :

    undefined reference to `Foo::FOO1'
    

    这表明它是ODR使用的。是哪个?


    a real (and more complex) case :

    • 代码编译并与-O2链接良好
    • 代码得到了上面的结果 undefined reference 错误LinkTimeOptimization( -O2 -flto

    -O )和链接时间优化( -flto

    1 回复  |  直到 7 年前