std::variant 可以定义多少种类型?

How many types can std::variant define?

我了解到 c++17 中有一个 std::variant 类型。 看起来变体容器不支持预定义的数据类型,但对于每个变体类型,用户可以定义她自己的数据类型集。

std::variant<int, float> v;

我想知道,类型列表可以有多长?该库是否以 Aleksandrescu 方式为最大数量的参数预定义模板,或者编译器支持的变体并且类型数量不受限制?

模板参数的最大数量受编译器实现的限制。

The C++ standard says:

Because computers are finite, C ++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This docu mentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

...

Template arguments in a template declaration [1024]