C++11 auto:如果它得到一个常量引用怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7138588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++11 auto: what if it gets a constant reference?
提问by minjang
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
:
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.
我预计这my_foo
将是对 的常量引用Foo
,它是函数的返回类型。但是,类型auto
是Foo
,而不是引用。此外,my_foo
是通过复制创建的g_temp
。这种行为对我来说并不那么明显。
In order to get the reference to Foo
, I needed to write like this:
为了获得对 的引用Foo
,我需要这样写:
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?
问题:为什么auto
将 的返回类型推导出GetFoo
为对象,而不是引用?
采纳答案by someguy
Read this article: Appearing and Disappearing consts in C++
阅读这篇文章:在 C++ 中出现和消失常量
Type deduction for auto variables in C++0x is essentially the same as for template parameters. (As far as I know, the only difference between the two is that the type of auto variables may be deduced from initializer lists, while the types of template parameters may not be.) Each of the following declarations therefore declare variables of type int (never const int):
C++0x 中自动变量的类型推导本质上与模板参数相同。(据我所知,两者的唯一区别是 auto 变量的类型可以从初始化列表中推导出来,而模板参数的类型可能不是。)因此,以下每个声明都声明了 int 类型的变量(从不常量 int):
auto a1 = i;
auto a2 = ci;
auto a3 = *pci;
auto a4 = pcs->i;
During type deduction for template parameters and auto variables, only top-level consts are removed. Given a function template taking a pointer or reference parameter, the constness of whatever is pointed or referred to is retained:
在模板参数和自动变量的类型推导期间,仅删除顶级常量。给定一个带有指针或引用参数的函数模板,所指向或引用的任何内容的常量性都被保留:
template<typename T>
void f(T& p);
int i;
const int ci = 0;
const int *pci = &i;
f(i); // as before, calls f<int>, i.e., T is int
f(ci); // now calls f<const int>, i.e., T is const int
f(*pci); // also calls f<const int>, i.e., T is const int
This behavior is old news, applying as it does to both C++98 and C++03. The corresponding behavior for auto variables is, of course, new to C++0x:
这种行为是旧闻,适用于 C++98 和 C++03。自动变量的相应行为当然是 C++0x 的新特性:
auto& a1 = i; // a1 is of type int&
auto& a2 = ci; // a2 is of type const int&
auto& a3 = *pci; // a3 is also of type const int&
auto& a4 = pcs->i; // a4 is of type const int&, too
Since you can retain the cv-qualifier if the type is a reference or pointer, you can do:
如果类型是引用或指针,则可以保留 cv 限定符,因此可以执行以下操作:
auto& my_foo2 = GetFoo();
Instead of having to specify it as const
(same goes for volatile
).
而不必将其指定为const
(同样适用于volatile
)。
Edit:As for why auto
deduces the return type of GetFoo()
as a value instead of a reference (which was your main question, sorry), consider this:
编辑:至于为什么auto
将返回类型推导出GetFoo()
为值而不是引用(这是您的主要问题,抱歉),请考虑:
const Foo my_foo = GetFoo();
The above will create a copy, since my_foo
is a value. If auto
were to return an lvalue reference, the above wouldn't be possible.
以上将创建一个副本,因为它my_foo
是一个值。如果auto
要返回左值引用,则上述情况是不可能的。