C++11 auto:如果它获得常量引用怎么办?
请看一下下面的简单代码:
class Foo
{
public:
Foo(){}
~Foo(){}
Foo(const Foo&){}
Foo& operator=(const Foo&) { return *this; }
};
static Foo g_temp;
const Foo& GetFoo() { return g_temp; }
我尝试像这样使用 auto
:
auto my_foo = GetFoo();
我期望 my_foo
将是对 Foo
的常量引用,这是函数的返回类型。但是,auto
的类型是 Foo
,而不是引用。此外,my_foo
是通过复制g_temp
创建的。这种行为对我来说并不那么明显。
为了获得对 Foo
的引用,我需要这样写:
const auto& my_foo2 = GetFoo();
auto& my_foo3 = GetFoo();
问题:为什么 auto
推导出 的返回类型GetFoo 作为对象,而不是引用?
Please take a look at the following simple code:
class Foo
{
public:
Foo(){}
~Foo(){}
Foo(const Foo&){}
Foo& operator=(const Foo&) { return *this; }
};
static Foo g_temp;
const Foo& GetFoo() { return g_temp; }
I tried to use auto
like this:
auto my_foo = GetFoo();
I expected that my_foo
will be a constant reference to Foo
, which is the return type of the function. However, the type of auto
is Foo
, not the reference. Furthermore, my_foo
is created by copying g_temp
. This behavior isn't that obvious to me.
In order to get the reference to Foo
, I needed to write like this:
const auto& my_foo2 = GetFoo();
auto& my_foo3 = GetFoo();
Question: Why does auto
deduce the return type of GetFoo
as an object, not a reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读这篇文章:C++ 中出现和消失的常量
由于如果类型是引用或指针,您可以保留 cv 限定符,因此您可以这样做:
而不必将其指定为
const
(对于也是如此)易失性
)。编辑:至于为什么
auto
将GetFoo()
的返回类型推导出为值而不是引用(这是您的主要问题,抱歉),考虑一下:上面的代码将创建一个副本,因为
my_foo
是一个值。如果auto
返回左值引用,则上述情况将不可能实现。Read this article: Appearing and Disappearing consts in C++
Since you can retain the cv-qualifier if the type is a reference or pointer, you can do:
Instead of having to specify it as
const
(same goes forvolatile
).Edit: As for why
auto
deduces the return type ofGetFoo()
as a value instead of a reference (which was your main question, sorry), consider this:The above will create a copy, since
my_foo
is a value. Ifauto
were to return an lvalue reference, the above wouldn't be possible.您可以采用 MSVC 技术文档:
您不需要函数来实现类似的结果。考虑:
avar
的类型将是int
,而不是const int &
或int &
。要更深入地了解这一点,我们可以访问 cppreference.com 并参考与模板参数推导类比。
我们倾向于直观地使用模板,在它起作用之前我们从来不会真正关心模板参数推导规则。这些规则实际上相当复杂。但是我们可以将“auto 的虚构模板”设为非虚构,并测试如果我们向模板提供
auto i = expr;
会发生什么。在我们的例子中,模板可能如下所示:让我们向它提供我们感兴趣的类型的变量:
输出是什么?
正如 MSVC 文档所预测的那样,
auto
也会发生同样的事情:)如果您想玩的话,这是代码。
这也是 MS 网站上的示例(如果有一天它消失了)
You can take simple answer as granted by MSVC Technical documentation:
You don't need a function to achieve similar results. Consider:
The type of
avar
will beint
notconst int &
norint &
.To go a bit deeper into this we can go to cppreference.com and refer to template argument deduction analogy.
We tend to intuitively use templates and until it works we never really bother about template argument deduction rules. These rules are in fact quite complex. But we can make the "imaginary template for auto" non-imaginary and test what would have happened if we provided
auto i = expr;
to the template. In our case the template may look like this:Let's feed it with variables with types of our interest:
And the what's at the output?
The same thing would have happened with
auto
as predicted by MSVC docs :)Here's the code if you wanted to play.
Here's also example from MS website if it disappeared one day