get for variants fail under clang++ but not g++

后端 未结 1 1311
北荒
北荒 2020-12-20 10:51

The following code:

variant x = \"abc\";
cout << get(x) << \"\\n\";

works fine under g++ (version 7

1条回答
  •  醉话见心
    2020-12-20 11:31

    This is caused by clang bug 31852 (and also 33222), whose reproduction courtesy of Jonathan Wakely should look very relevant:

    template auto get(V&) { }
    
    template
    class variant
    {
        template friend auto get(V&);
    };
    
    int main()
    {
      variant v{};
      get(v); // error: ambiguous 
    }
    

    clang doesn't properly recognize friend declarations that have placeholder types. Which is exactly how libstdc++ implements std::get:

    // Returns the typed storage for __v.
    template
    constexpr decltype(auto) __get(_Variant&& __v)
    {
        return __get(std::in_place_index<_Np>, std::forward<_Variant>(__v)._M_u);
    }
    

    this accesses a private member of variant, but this function is properly declared a friend:

    template
    friend constexpr decltype(auto) __detail::__variant::__get(_Vp&& __v);
    

    libstdc++'s implementation is valid, clang just doesn't think __get is a friend.

    0 讨论(0)
提交回复
热议问题