`const auto` 有什么意义吗?

发布于 2024-10-18 21:52:46 字数 212 浏览 2 评论 0原文

我认为这个问题已经很清楚了。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

软的没边 2024-10-25 21:52:46
const auto x = expr;

不同于

auto x = expr;

as

const X x = expr;

不同于 因此

X x = expr;

,请经常使用 const autoconst auto&,就像没有 auto 时一样。

重载解析不受返回类型的影响:左值上的 const 或无 const x 不会影响 expr 中调用的函数

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.

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.

素衣风尘叹 2024-10-25 21:52:46

也许您混淆了 const_iteratorconst iterator。第一个迭代 const 元素,第二个根本无法迭代,因为您不能在其上使用 ++ 和 -- 运算符。

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

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

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.

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

const auto end = container.end();
for (auto i = container.begin(); i != end; ++i) { ... }
美人迟暮 2024-10-25 21:52:46

假设您有两个模板:

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

auto 将推导出类型,并且变量将具有与参数 u 相同的类型(如 // 1 中所示) case),const auto 将使变量与 // 2 case 中的参数 u 具有相同的类型。所以 const auto 只需强制使用 const 限定符即可。

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.

梦明 2024-10-25 21:52:46

编译器推导自动限定符的类型。如果推导类型为 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 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:

  • 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 const for auto.
乖乖兔^ω^ 2024-10-25 21:52:46

const autoauto 不同,但是 const auto& 并不总是与 auto& 不同。

首先是一个使用 const auto 的简单示例:

const int a = 10;
int b = a;
const int c = a;
auto d = a; // this is equivalent to int d = a;

d = 11; // this works since d is not const

这里我们可以将 const int 分配给 intconst int (< code>b 和 c),因此在 auto d 中,编译器不会强制 d 为 const。如果我们希望 d 为 const,我们就必须说 const auto

但是,如果不注入 const 的编译失败,则会自动添加 const (但是如果尝试使用该变量,就好像 const 是没有注入,就像下面的 d = 11 一样,此时编译当然会失败):

const int a = 10;
// int& b = a; // won't compile, we cannot assign const int to reference to (non-const) int
const int& c = a;
auto& d = a; // this is equivalent to const auto& d = a;

// d = 11; // this doesn't compile, since d is a reference to const

const auto is different from auto, however const auto& is not always different from auto&.

First a simple example with const auto:

const int a = 10;
int b = a;
const int c = a;
auto d = a; // this is equivalent to int d = a;

d = 11; // this works since d is not const

Here we can assign const int to either int or const int (b and c), so in auto d a compiler wouldn't force d to be const. If we wanted d to be const we would have to say const 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 if const is not injected, like in d = 11 below, compilation would of course fail at that point):

const int a = 10;
// int& b = a; // won't compile, we cannot assign const int to reference to (non-const) int
const int& c = a;
auto& d = a; // this is equivalent to const auto& d = a;

// d = 11; // this doesn't compile, since d is a reference to const
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文