C++ auto 关键字.为什么是魔法? [英] C++ auto keyword. Why is it magic?

查看:36
本文介绍了C++ auto 关键字.为什么是魔法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我用来学习 C++ 的所有材料中,auto 一直是一个奇怪的存储持续时间说明符,没有任何用途.但就在最近,我遇到了将其用作类型名称的代码.出于好奇,我尝试了它,它假定我碰巧分配给它的任何类型!

From all the material I used to learn C++, auto has always been a weird storage duration specifier that didn't serve any purpose. But just recently, I encountered code that used it as a type name in and of itself. Out of curiosity I tried it, and it assumes the type of whatever I happen to assign to it!

突然出现 STL 迭代器,以及任何使用模板的东西,编写起来都容易 10 倍.感觉就像我在使用 Python 这样的有趣"语言.

Suddenly STL iterators and, well, anything at all that uses templates is 10 fold easier to write. It feels like I'm using a 'fun' language like Python.

这个关键字在我的一生中在哪里?你会说它是 Visual Studio 独有的还是不可移植的,这会打破我的梦想吗?

Where has this keyword been my whole life? Will you dash my dreams by saying it's exclusive to visual studio or not portable?

推荐答案

auto 是 C++继承"的关键字来自几乎永远存在但实际上从未使用过的 C 语言,因为只有两种可能的条件:要么不被允许,要么默认情况下假设它.

auto was a keyword that C++ "inherited" from C that had been there nearly forever, but virtually never used because there were only two possible conditions: either it wasn't allowed, or else it was assumed by default.

使用 auto 表示推导的类型是 C++11 的新功能.

The use of auto to mean a deduced type was new with C++11.

同时,auto x = initializerinitializer的类型推导出x的类型,和模板类型推导一样适用于函数模板.考虑这样的函数模板:

At the same time, auto x = initializer deduces the type of x from the type of initializer the same way as template type deduction works for function templates. Consider a function template like this:

template<class T>
int whatever(T t) { 
    // point A
};

在 A 点,根据传递给 whatever 的参数值,类型已分配给 T.当你做 auto x = initializer; 时,同样的类型推导被用来根据 initializer 的类型来确定 x 的类型,该类型用于初始化它.

At point A, a type has been assigned to T based on the value passed for the parameter to whatever. When you do auto x = initializer;, the same type deduction is used to determine the type for x from the type of initializer that's used to initialize it.

这意味着编译器需要实现 auto 的大多数类型推导机制已经存在并用于任何编译器上的模板,甚至尝试实现 C++98/03.因此,对于基本上所有的编译器团队来说,添加对 auto 的支持显然相当容易——它添加得非常快,而且似乎也很少有与它相关的错误.

This means that most of the type deduction mechanics a compiler needs to implement auto were already present and used for templates on any compiler that even sort of attempted to implement C++98/03. As such, adding support for auto was apparently fairly easy for essentially all the compiler teams--it was added quite quickly, and there seem to have been few bugs related to it either.

最初编写此答案时(在 2011 年,在 C++ 11 标准上墨迹未干之前)auto 已经非常便携.如今,它在所有主流编译器之间完全可移植.避免它的唯一明显原因是,如果您需要编写与 C 编译器兼容的代码,或者您有特定的需要针对某些您知道不支持它的小众编译器(例如,一些人仍在编写代码适用于使用 Borland、Watcom 等编译器的 MS-DOS,这些编译器几十年来没有进行过重大升级).如果您使用的是任何主流编译器的合理当前版本,则完全没有理由避免使用它.

When this answer was originally written (in 2011, before the ink was dry on the C++ 11 standard) auto was already quite portable. Nowadays, it's thoroughly portable among all the mainstream compilers. The only obvious reasons to avoid it would be if you need to write code that's compatible with a C compiler, or you have a specific need to target some niche compiler that you know doesn't support it (e.g., a few people still write code for MS-DOS using compilers from Borland, Watcom, etc., that haven't seen significant upgrades in decades). If you're using a reasonably current version of any of the mainstream compilers, there's no reason to avoid it at all though.

该标准的最新修订版添加了一些可以使用 auto 的新位置.从 C++14 开始,您可以使用 auto 作为 lambda 的参数类型:

More recent revisions of the standard have added a few new places that auto can be used. Starting with C++14, you can use auto for the type of a parameter to a lambda:

    [](auto s) { return s + 1; }

这与上面的例子做的事情本质上是一样的——尽管它没有明确使用template语法,但这基本上是一个推断参数类型并实例化模板的模板超过那种类型.

This does essentially the same thing as the example above--even though it doesn't explicitly use template syntax, this is basically a template that deduces the type of the parameter, and instantiates the template over that type.

这非常方便和有用,以至于在 C++20 中,为普通函数添加了相同的功能,而不仅仅是 lambda.

That was convenient and useful enough that in C++20, the same capability was added for normal functions, not just lambdas.

但是,就像以前一样,所有这一切都归结为使用与自 C++98 以来的函数模板相同的基本类型推导机制.auto 允许它在更多地方使用,更方便,但底层的繁重工作保持不变.

But, just as before all of this really comes down to using the same basic type deduction mechanism as we've had for function templates since C++98. auto allows that to be used in more places, and more conveniently, but the underlying heavy lifting remains the same.

这篇关于C++ auto 关键字.为什么是魔法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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