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).
我认为这个问题很清楚。自动关键字自动检测const-ness,或者总是返回一个非const类型,即使有eg。函数的两个版本(一个返回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
.
为了便于记录,我在for循环之前确实使用了const auto end = some_container.end(),但是我不知道这是否必要,甚至与普通的auto有什么不同。
4 个解决方案
#1
20
Maybe you are confusing const_iterator
and 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_iterator和const迭代器。第一个遍历const元素,第二个根本不能迭代,因为不能使用操作符++和-。
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) { ... }
#2
73
const auto x = expr;
differs from
不同于
auto x = expr;
as
作为
const X x = expr;
differs from
不同于
X x = expr;
So use const auto
and const auto&
a lot, just like you would if you didn't have auto
.
所以要经常使用const auto和const auto&就像你没有auto的时候一样。
Overload resolution is not affected by return type: const
or no const
on the lvalue x
does not affect what functions are called in expr
.
重载解析不受返回类型的影响:lvalue x上的const或no const不影响expr中调用的函数。
#3
7
Consider you have two templates:
假设您有两个模板:
template<class U> void f1( U& u ); // 1
template<class U> void f2( const U& u ); // 2
auto
will deduce type and the variable will have the same type as the parameter u
(as in the // 1
case), const auto
will make variable the same type as the parameter u
has in the // 2
case. So const auto
just force const
qualifier.
auto将演绎类型,变量将与参数u具有相同的类型(如// 1例中),const auto将使变量与u在// 2情况下的参数相同。所以,const auto就是force const qualifier。
#4
0
Compiler deduces the type for auto qualifier. If deduced type is some_type
, const auto
will be converted to const some_type
. However, a good compiler will examine the whole scope of auto
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:
编译器推断自动限定符的类型。如果导出的类型是some_type,则const自动将转换为const some_type。但是,一个好的编译器会检查自动变量的整个范围,并发现它的值是否在任何地方发生变化。如果不是,编译器本身会推断出这样的类型: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 it's scope it is changed.
- 防止编码错误。你想让这个变量不改变,但是在它的作用域内,它改变了。
- Code readability is improved.
- 提高代码的可读性。
- You help the compiler if for some reason it doesn't deduce
const
forauto
. - 你帮助编译器,如果出于某种原因,它没有推出const for auto。
#1
20
Maybe you are confusing const_iterator
and 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_iterator和const迭代器。第一个遍历const元素,第二个根本不能迭代,因为不能使用操作符++和-。
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) { ... }
#2
73
const auto x = expr;
differs from
不同于
auto x = expr;
as
作为
const X x = expr;
differs from
不同于
X x = expr;
So use const auto
and const auto&
a lot, just like you would if you didn't have auto
.
所以要经常使用const auto和const auto&就像你没有auto的时候一样。
Overload resolution is not affected by return type: const
or no const
on the lvalue x
does not affect what functions are called in expr
.
重载解析不受返回类型的影响:lvalue x上的const或no const不影响expr中调用的函数。
#3
7
Consider you have two templates:
假设您有两个模板:
template<class U> void f1( U& u ); // 1
template<class U> void f2( const U& u ); // 2
auto
will deduce type and the variable will have the same type as the parameter u
(as in the // 1
case), const auto
will make variable the same type as the parameter u
has in the // 2
case. So const auto
just force const
qualifier.
auto将演绎类型,变量将与参数u具有相同的类型(如// 1例中),const auto将使变量与u在// 2情况下的参数相同。所以,const auto就是force const qualifier。
#4
0
Compiler deduces the type for auto qualifier. If deduced type is some_type
, const auto
will be converted to const some_type
. However, a good compiler will examine the whole scope of auto
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:
编译器推断自动限定符的类型。如果导出的类型是some_type,则const自动将转换为const some_type。但是,一个好的编译器会检查自动变量的整个范围,并发现它的值是否在任何地方发生变化。如果不是,编译器本身会推断出这样的类型: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 it's scope it is changed.
- 防止编码错误。你想让这个变量不改变,但是在它的作用域内,它改变了。
- Code readability is improved.
- 提高代码的可读性。
- You help the compiler if for some reason it doesn't deduce
const
forauto
. - 你帮助编译器,如果出于某种原因,它没有推出const for auto。