`const auto` 有什么意义吗?
我认为这个问题已经很清楚了。 auto
关键字会自动检测 const 性,还是始终返回非 const 类型,即使存在例如。函数的两个版本(一个返回 const,另一个不返回)。
仅供记录,我确实在 for 循环之前使用了 const auto end = some_container.end() ,但我不知道这是否有必要,甚至与正常的 auto 不同/代码>。
I think the question is clear enough. Will the auto
keyword auto-detect const-ness, or always return a non-const type, even if there are eg. two versions of a function (one that returns const
and the other that doesn't).
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不同于
as
不同于 因此
,请经常使用
const auto
和const auto&
,就像没有auto
时一样。重载解析不受返回类型的影响:左值上的
const
或无const
x
不会影响expr 中调用的函数
。differs from
as
differs from
So use
const auto
andconst auto&
a lot, just like you would if you didn't haveauto
.Overload resolution is not affected by return type:
const
or noconst
on the lvaluex
does not affect what functions are called inexpr
.也许您混淆了
const_iterator
和const iterator
。第一个迭代 const 元素,第二个根本无法迭代,因为您不能在其上使用 ++ 和 -- 运算符。请注意,您很少从
container.end()
进行迭代。通常你会使用:Maybe you are confusing
const_iterator
andconst iterator
. The first one iterates over const elements, the second one cannot iterate at all because you cannot useoperators
++ and -- on it.Note that you very seldom iterate from the
container.end()
. Usually you will use:假设您有两个模板:
auto
将推导出类型,并且变量将具有与参数u
相同的类型(如// 1
中所示) case),const auto
将使变量与// 2
case 中的参数u
具有相同的类型。所以const auto
只需强制使用const
限定符即可。Consider you have two templates:
auto
will deduce type and the variable will have the same type as the parameteru
(as in the// 1
case),const auto
will make variable the same type as the parameteru
has in the// 2
case. Soconst auto
just forceconst
qualifier.编译器推导自动限定符的类型。如果推导类型为
some_type
,则const auto
将转换为const some_type
。然而,一个好的编译器会检查 auto 变量的整个范围,并查找它的值是否在任何地方发生变化。如果不是,编译器本身将推断出如下类型:auto
->const some_type
。我已经在 Visual studio Express 2012 中尝试过这一点,并且在两种情况下生成的机器代码是相同的,我不确定每个编译器都会这样做。但是,使用
const auto
是一个很好的做法,原因有以下三个:auto 推导出 const
,那么您就可以帮助编译器。Compiler deduces the type for the auto qualifier. If a deduced type is
some_type
,const auto
will be converted toconst some_type
. However, a good compiler will examine the whole scope ofauto
variable 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 auto
for three reasons:const
forauto
.const auto
与auto
不同,但是const auto&
并不总是与auto&
不同。首先是一个使用
const auto
的简单示例:这里我们可以将
const int
分配给int
或const int
(< code>b 和c
),因此在auto d
中,编译器不会强制d
为 const。如果我们希望d
为 const,我们就必须说const auto
。但是,如果不注入 const 的编译失败,则会自动添加 const (但是如果尝试使用该变量,就好像 const 是没有注入,就像下面的 d = 11 一样,此时编译当然会失败):
const auto
is different fromauto
, howeverconst auto&
is not always different fromauto&
.First a simple example with
const auto
:Here we can assign
const int
to eitherint
orconst int
(b
andc
), so inauto d
a compiler wouldn't forced
to be const. If we wantedd
to be const we would have to sayconst auto
.However if the compilation without injecting
const
would fail,const
would be automatically added (but then if one tries to use the variable as ifconst
is not injected, like ind = 11
below, compilation would of course fail at that point):