c++ - 在编译时获取 std::variant 类型
问题描述
假设我们在某处定义了:
using mytype_t = std::variant<int, float>;
我想检索可以存储的所有可能类型mytype_t
。我已经检查了参考。没有定义我可以使用的成员类型(例如:)mytype_t::value_types
。
我正在尝试做这样的事情:
using myvectype_t = std::variant<std::vector<mytype_t::value_types>...>;
解决方案
你不需要一个特殊的成员,因为信息在类型中是正确的:
#include <variant>
#include <iostream>
#include <vector>
#include <type_traits>
template <typename T>
struct foo;
template <typename...V>
struct foo< std::variant<V...>> {
using type = std::variant< std::vector<V> ...>;
};
int main() {
using vari = std::variant<int,double>;
using varivect = std::variant< std::vector<int>, std::vector<double>>;
std::cout << std::is_same<foo<vari>::type,varivect>::value;
}
输出:
1
推荐阅读
- javascript - 如何在两个日期范围内创建周/月/季度列表
- javascript - 如何使用jQuery ajax更改功能根据同一行表自动更改选择
- pointers - 简单的 Go 指针解释
- javascript - 文件输入第二次无法识别文件上传
- lambda - Java8 lambda:以相反的顺序对流进行排序?
- java - 无法正确读取和显示文件内容
- swift - 从元类型获取类型
- asp.net - 错误“没有'Access-Control-Allow-Origin'”,但cors允许一切
- python-3.x - PyQt5 中从“a”到 Qt.Key_A
- hadoop-yarn - 在 Yarn 集群上提交 Flink 作业以从上一个保存点恢复其状态