使用auto作为模板参数
Using auto as a template parameter
我正试图使用GCC 4.7.1并设置-std=c++11
标志来编译以下内容:
std::map<std::string, auto> myMap;
我正试图创建一个对象来包含大量各种类型的Json数据(int string、bool)以及子结构(list、map),所以我无法在编译时声明字段值的类型,所以我想对它使用auto
关键字。
然而,当我试图编译它时,我得到了以下
error: invalid use of ‘auto’
error: template argument 2 is invalid
error: template argument 4 is invalid
error: unable to deduce ‘auto’ from ‘<expression error>’
有没有一种特殊的方法可以使用auto
作为模板参数,或者这是不可能的?
我认为您想要的是boost::any。
std::map<std::string, boost::any> myMap;
auto
是在编译时评估的,不能用作动态运行时类型。
这根本不可能。auto
背后的类型必须从某种东西中推导出来。最接近这一点的方法是使用带有某种表达式的decltype
。
std::map<std::string, decltype(some expression)> myMap;
但这里的decltype
解析为一个类型,您不能在编译时更改它。
相关文章:
- 将函数参数类型声明为 auto
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- C++/11 auto 关键字是在更有效时推导参数进行按引用传递,还是始终按值传递?
- C++17.处理使用 auto 相关的模板参数.代码排序困难
- 无法使用 auto 来参数化true_type来检测 T::value()
- 将 auto 与生成元组的可变参数模板一起使用
- 使用"auto"的部分专用模板推断失败的模板参数推断
- 无法通过 std::ref() 使用 auto& 参数调用 std::invoke()
- Hana::Tuple to Auto && ...参数
- 省略C++可变参数 lambda 中的"auto"关键字?
- 在 QGraphicsScene 中拖动 QPixmap:如何避免 lambda 参数中不允许'auto'
- 在模板参数中使用 auto:一些使用示例和..如何使其与恒定大小的 C 数组一起使用
- 如何用'auto'单词推导出哪种类型的参数编写函数?
- 将 auto 作为参数传递(用于指向对象的指针)
- 如何处理"警告:在参数声明中使用'auto'仅适用于 -fconcepts"
- 是在模板模板参数的情况下,是由DECTTYPE(AUTO)推出的参考非类型模板参数
- lambda 中的跨平台"auto"关键字用法:integral_constant作为函数参数
- C 11 std :: bind and auto的编译错误,用于回调函数参数
- 有没有一种方法可以在C++中将auto作为参数传递
- 使用auto作为模板参数