相关疑难解决方法(0)

std::variant 和不完整类型:它是如何工作的?

我确实明白这std::variant适用于不完整的类型。但是,我不明白它是如何工作的,因为根据我的理解,std::variant必须需要它所持有的类型的最大大小。

那么,为什么这段代码不能用s1and编译s2。怎样才能让它发挥作用呢std::variant

#include <variant>
#include <vector>
#include <type_traits>
#include <typeinfo>
#include <iostream>

struct Rect;
struct Circle;

using Shape = std::variant<Rect, Circle>;

template<typename C>
struct S {static constexpr auto s = sizeof(C);};

constexpr auto s1 = S<Rect>::s;
constexpr auto s2 = sizeof(Rect);

struct Circle{};
struct Rect{
    std::vector<Shape> shapes;
};

int main() {}
Run Code Online (Sandbox Code Playgroud)

c++ variant

2
推荐指数
1
解决办法
1344
查看次数

标签 统计

c++ ×1

variant ×1