为什么不能使用 auto 作为模板类型参数(例如 std::array<auto, 5>)? [英] Why can&#39;t you use auto as a template type argument (e.g. std::array&lt;auto, 5&gt;)?

查看:48
本文介绍了为什么不能使用 auto 作为模板类型参数(例如 std::array<auto, 5>)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不允许这样做,例如:

Why is this not allowed, for example:

std::array<auto, 5> myArray {

};

这会让我的生活变得更轻松,因为我可以在数组中存储多种数据类型.我确定有一个合乎逻辑的解释,只是想知道它是什么.

It would make my life so much easier, as I would be allowed to store multiple data-types inside the array. I'm sure there's a logical explanation, just wondered what it was.

推荐答案

auto 用于从表达式中推导出一种类型.使用您建议的语法无济于事,因为只能在容器中存储一种类型.如果您需要一种在容器中存储任何类型的方法,请查看boost::any,以便您可以使用

auto is used to deduce one type from an expression. Using your suggested syntax would not help because exactly one type can be stored in a container. If you need a way to store any type in the container, take a look at boost::any, so you can use

std::array<boost::any, 5> myArray;

这篇关于为什么不能使用 auto 作为模板类型参数(例如 std::array<auto, 5>)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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