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

标签 c++ variant c++17

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

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]

关于c++ - std::variant 可以定义多少种类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791916/

相关文章:

c++ - 如何分配两个二维数组

c++ - boost::variant 类型转换

c++ - 如果其中一种变体类型,如何启用模板功能?

c++ - 重载模板 <typename ...> 类的函数模板

c++ - 常量表达式中的静态成员访问

c++ - gcc 原子内置函数提供哪些异常保证?

c++ - 在 C++ 中动态加载类值

c++ - 在二进制文件中写入对齐数据

c++ - C++ 中的求和类型

C++:std::invoke 是否需要完美转发?