I do understand that std::variant
works with incomplete type. However, I don't understand how it can works because, in my understanding, std::variant
must need the maximum size of the types it holds.
So, why does this code does not compile with s1
and s2
. How can make it works like 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() {}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…