C++ `const auto` 有什么意义吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5070521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:19:24  来源:igfitidea点击:

does `const auto` have any meaning?

c++c++11const

提问by rubenvb

I think the question is clear enough. Will the autokeyword auto-detect const-ness, or always return a non-const type, even if there are eg. two versions of a function (one that returns constand the other that doesn't).

我认为这个问题已经足够清楚了。将auto关键字自动检测常量性,或总是返回一个非const类型,即使有如。函数的两个版本(一个返回const,另一个不返回)。

Just for the record, I do use const auto end = some_container.end()before my for-loops, but I don't know if this is necessary or even different from normal auto.

只是为了记录,我确实const auto end = some_container.end()在 for 循环之前使用过,但我不知道这是否有必要,甚至与 normal 不同auto

采纳答案by Benoit

Maybe you are confusing const_iteratorand const iterator. The first one iterates over const elements, the second one cannot iterate at all because you cannot use operators++ and -- on it.

也许你很困惑const_iteratorconst iterator。第一个迭代 const 元素,第二个根本无法迭代,因为您不能operators在其上使用++ 和 -- 。

Note that you very seldom iterate from the container.end(). Usually you will use:

请注意,您很少从container.end(). 通常你会使用:

const auto end = container.end();
for (auto i = container.begin(); i != end; ++i) { ... }

回答by antonakos

const auto x = expr;

differs from

不同于

auto x = expr;

as

作为

const X x = expr;

differs from

不同于

X x = expr;

So use const autoand const auto&a lot, just like you would if you didn't have auto.

所以使用const autoconst auto&很多,就像你没有auto.

Overload resolution is not affected by return type: constor no conston the lvalue xdoes not affect what functions are called in expr.

重载解析不受返回类型的影响:constconst左值上的nox不影响expr.

回答by Kirill V. Lyadvinsky

Consider you have two templates:

考虑您有两个模板:

template<class U> void f1( U& u );       // 1
template<class U> void f2( const U& u ); // 2

autowill deduce type and the variable will have the same type as the parameter u(as in the // 1case), const autowill make variable the same type as the parameter uhas in the // 2case. So const autojust force constqualifier.

auto将推导类型并且变量将具有与参数相同的类型u(如// 1案例中),const auto将使变量与案例中的参数u具有相同的类型// 2。所以const auto只是强制const限定符。

回答by BJovke

Compiler deduces the type for the auto qualifier. If a deduced type is some_type, const autowill be converted to const some_type. However, a good compiler will examine the whole scope of autovariable and find if the value of it changes anywhere. If not, compiler itself will deduce type like this: auto-> const some_type. I've tried this in Visual studio express 2012 and machine code produced is the same in both cases, I'm not sure that each and every compiler will do that. But, it is a good practice to use const autofor three reasons:

编译器推导出 auto 限定符的类型。如果推导类型为some_typeconst auto则将转换为const some_type。然而,一个好的编译器会检查auto变量的整个范围,并发现它的值是否在任何地方发生了变化。如果不是,编译器本身会推导出这样的类型:auto-> const some_type。我已经在 Visual Studio express 2012 中尝试过这个,并且在这两种情况下生成的机器代码是相同的,我不确定每个编译器都会这样做。但是,const auto出于以下三个原因使用它是一个好习惯:

  • Preventing coding errors. You intended for this variable not to change but somehow somewhere in its scope, it is changed.
  • Code readability is improved.
  • You help the compiler if for some reason it doesn't deduce constfor auto.
  • 防止编码错误。你打算让这个变量不改变,而是在它的范围内以某种方式改变了。
  • 代码可读性得到提高。
  • 如果由于某种原因它没有推导出constfor ,您可以帮助编译器auto