Using std::visit on a class inheriting from std::variant - libstdc++ vs libc++

前端 未结 3 443
忘了有多久
忘了有多久 2021-01-01 11:28

Consider the following code snippet:

struct v : std::variant> { };

int main()
{
    std::visit([](auto){ }, v{0});
}
         


        
3条回答
  •  我在风中等你
    2021-01-01 12:00

    Looks like it is a bug in gcc implementation. According to cppreference, it is called as if calling invoke on a std::get. std::get<> is defined for anything which is convertible to std::variant (since it accepts a std::variant argument by forwarding reference). Your structure is convertible to std::variant, and so std::get itself works on your structure in gcc.

    The fact that the gcc implementation chose to use a std::variant_size as part of its implementation of visit is their implementation detail, and the fact that it doesn't (and shouldn't) work for your struct is irrelevant.

    Conclusion: It is a bug in gcc due to an oversight in implementation.

提交回复
热议问题