在从std :: variant继承的类上使用std :: visit-libstdc ++ vs libc ++ [英] Using std::visit on a class inheriting from std::variant - libstdc++ vs libc++

查看:107
本文介绍了在从std :: variant继承的类上使用std :: visit-libstdc ++ vs libc ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码段:

struct v : std::variant<int, std::vector<v>> { };

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

    带有-stdlib=libc++ -std=c++2a
  • clang ++ 7编译代码;

    • clang++ 7 with -stdlib=libc++ -std=c++2a compiles the code;

      g ++ 9无法编译代码,并出现以下错误:

      g++ 9 with -std=c++2a fails to compile the code, with the following error:

      /opt/compiler-explorer/gcc-trunk-20180711/include/c ++/9.0.0/variant:94:29: 错误:嵌套名称中使用了不完整的类型'std :: variant_size' 说明符

      /opt/compiler-explorer/gcc-trunk-20180711/include/c++/9.0.0/variant:94:29: error: incomplete type 'std::variant_size' used in nested name specifier

       inline constexpr size_t variant_size_v = variant_size<_Variant>::value;
      
                               ^~~~~~~~~~~~~~
      

    • godbolt.org上的实时示例

      • 两个实现均符合标准吗?

      如果不是,什么实现在这里是正确的,为什么?

      If not, what implementation is correct here, and why?

      推荐答案

      [variant .visit]在C ++ 17 中不使用variant_size_v,但在当前工作草案" rel ="noreferrer">版本更改.我看不到任何迹象表明LWG在进行更改之前已经对其进行了审核,但是自那以后它已经对该标准的这一部分进行了多次考察,并且尚未反对它,因此我将假定它已经在进行中了.需要事实.

      [variant.visit] in C++17 doesn't use variant_size_v, but it does in the current working draft as a result of an editorial change. I don't see any indication that LWG reviewed the change before it went in, but it has looked at this part of the standard several times since then and has yet to object to it, so I'm going to postulate that it is in fact required.

      与此同时,已提到LEWG的 LWG问题3052 将明确要求std::variant.解决该问题(一种方法或另一种方法)后,它也应解决此问题.

      Meanwhile, LWG issue 3052, which has been referred to LEWG, would explicitly require std::variant. When that issue is resolved - one way or the other - it should resolve this too.

      这篇关于在从std :: variant继承的类上使用std :: visit-libstdc ++ vs libc ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆