使用auto作为模板参数 [英] Using auto as a template parameter

查看:256
本文介绍了使用auto作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GCC 4.7.1编译下面的 -std = c ++ 11 标志设置:

  std :: map< std :: string,auto> myMap; 

我试图创建一个对象来包含大量的各种类型的Json数据字符串,bool)以及子结构(列表,映射),所以我不能在编译时声明字段值的类型,所以我想我会使用 auto

<$ p

$ p> 错误:无效使用'auto'
错误:模板参数2无效
错误:模板参数4无效
错误:无法推断'auto 'from'< expression error>'

是否有特殊的方法使用 auto 作为模板参数,还是不可能?

解决方案

正在寻找的是 boost :: any

  std :: map< std :: string,boost :: any> myMap; 

auto 可用作动态运行时类型。


I'm attempting to compile the following using GCC 4.7.1 with the -std=c++11 flag set:

std::map<std::string, auto> myMap;

I'm attempting to create an object to contain a large amount of Json data of various types (int string, bool) as well as sub-structures (list, map) so I can't declare the type of the field value at compile time, so I thought I'd use the auto keyword for it.

However, when I try to compile it, I get the following

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>’

Is there a special way to use auto as a template argument, or is it just not possible?

解决方案

I think what you are looking for is boost::any.

std::map<std::string, boost::any> myMap;

auto is evaluated during compile time and cannot be used as a dynamic run-time type.

这篇关于使用auto作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆