【问题标题】:How does the Compiler Know Whether to Call the const Overload?编译器如何知道是否调用 const 重载?
【发布时间】:2016-10-01 09:10:09
【问题描述】:

给定string foo,当我打电话时:

auto bar = foo.begin();

string::begin 有 2 个重载。一个返回string::iterator,另一个返回string::const_iterator。我如何知道bar 的类型?这是否仅基于foo 是否为const

【问题讨论】:

  • "这只是基于 foo 是否为 const 吗?"是的。
  • 如果您确定想要const_iterator,也可以致电cbegin

标签: c++ iterator constants overloading const-iterator


【解决方案1】:

这只是基于 foo 是否为 const 吗?

是的

auto 用于推断确切类型*


How to select iterator type using auto variable?间接同意这个答案。

*取自:How do I get a const_iterator using auto?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多