为什么默认情况下,lambda函数会丢弃推导的返回类型引用? [英] Why do lambda functions drop deduced return type reference by default?
问题描述
在C ++ 14中,为什么默认情况下具有推导的返回类型的lambda函数会从返回类型中删除引用? IIUC,因为具有推导的返回类型(没有显式的尾随返回类型)的C ++ 14 lambda函数的返回类型为auto
,这会删除引用(以及其他内容).
In C++14, why do lambda functions with a deduced return type drop references from the return type by default? IIUC, since C++14 lambda functions with a deduced return type (without an explicit trailing return type) have a return type of auto
, which drops references (among other things).
为什么要做出这个决定?在我看来,当那是您的return语句返回的内容时,删除引用是一个陷阱.
Why was this decision made? It seems to me like a gotcha to remove a reference when that's what your return statement returns.
此行为对我造成了以下讨厌的错误:
This behavior caused the following nasty bug for me:
class Int {
public:
Int(int i) : m_int{i} {}
int m_int;
};
class C {
public:
C(Int obj) : m_obj{obj} {}
const auto& getObj() { return m_obj; }
Int m_obj;
};
class D {
public:
D(std::function<const Int&()> f) : m_f{f} {}
std::function<const Int&()> m_f;
};
Int myint{5};
C c{myint};
D d{ [&c](){ return c.getObj(); } } // The deduced return type of the lambda is Int (with no reference)
const Int& myref = d.m_f(); // Instead of referencing myint, myref is a dangling reference; d.m_f() returned a copy of myint, which is subsequently destroyed.
在初始化d
时指定所需的返回类型可解决此问题:
Specifying the desired return type when initializing d
resolves the issue:
D d{ [&c]() -> const Int& { return c.getObj(); } }
有趣的是,即使auto
返回类型推论有意义,不是std::function<const Int&>
用返回非引用的函数来快乐地初始化std::function<const Int&>
的错误吗?我也通过明确编写来看到这一点:
Interestingly, even if the auto
return type deduction makes sense, isn't it a bug that std::function<const Int&>
gets happily initialized with a function that returns a non-reference? I see this also by writing explicitly:
D d{ [&c]() -> Int { return c.getObj(); } }
编译没有问题. (在Xcode 8
,clang 8.0.0
上)
which compiles without a problem. (on Xcode 8
, clang 8.0.0
)
推荐答案
我认为您绊脚的地方实际上是return c.getObj();
行中的表达式c.getObj()
.
I think the place you are stumbling is actually with the expression c.getObj()
in the line return c.getObj();
.
您认为表达式c.getObj()
具有类型const Int&
.但是,这是不正确的.表达式从不具有引用类型.正如Kerrek SB在评论中所指出的那样,有时我们会将表达式视为具有引用类型,这是节省冗长的捷径,但这会导致误解,因此,我认为了解实际情况很重要.
You think the expression c.getObj()
has type const Int&
. However that is not true; expressions never have reference type. As noted by Kerrek SB in comments, we sometimes talk about expressions as if they had reference type, as a shortcut to save on verbosity, but that leads to misconceptions so I think it is important to understand what is really going on.
在声明中使用引用类型(包括作为在getObj
声明中的返回类型)会影响被声明的事物的初始化方式,但是一旦被初始化,就不再有任何证据表明它最初是一个参考.
The use of a reference type in a declaration (including as a return type as in getObj
's declaration) affects how the thing being declared is initialized, but once it is initialized, there is no longer any evidence that it was originally a reference.
这是一个更简单的示例:
Here is a simpler example:
int a; int &b = a; // 1
与
int b; int &a = b; // 2
这两个代码与完全相同(除了decltype(a)
或decltype(b)
的结果,这对系统来说有点麻烦).在这两种情况下,表达式a
和b
都具有类型int
和值类别"lvalue",并表示相同的对象. a
不是真实对象"并且b
是某种伪装的指向a
的指针的情况并非如此.他们都处于平等地位.这是一个有两个名称的对象.
These two codes are exactly identical (except for the result of decltype(a)
or decltype(b)
which is a bit of a hack to the system). In both cases the expressions a
and b
both have type int
and value category "lvalue" and denote the same object. It's not the case that a
is the "real object" and b
is some sort of disguised pointer to a
. They are both on equal footing. It's one object with two names.
现在回到您的代码:除了访问权限外,表达式c.getObj()
的行为与c.m_obj
完全相同.类型为Int
,值类别为"lvalue".返回类型为getObj()
的&
仅指示这是一个左值,并且还将指定一个已经存在的对象(大约来说).
Going back to your code now: the expression c.getObj()
has exactly the same behaviour as c.m_obj
, apart from access rights. The type is Int
and the value category is "lvalue". The &
in the return type of getObj()
only dictates that this is an lvalue and it will also designate an object that already existed (approximately speaking).
因此,从return c.getObj();
推导的返回类型与return c.m_obj;
相同,后者与模板类型推导兼容(如其他地方所述),不是引用类型.
So the deduced return type from return c.getObj();
is the same as it would be for return c.m_obj;
, which -- to be compatible with template type deduction, as mentioned elsewhere -- is not a reference type.
NB.如果您了解了这篇文章,您还将理解为什么我不喜欢将引用"的教学法讲授为自动取消引用的伪装指针",这介于错误和危险之间.
NB. If you understood this post you will also understand why I don't like the pedagogy of "references" being taught as "disguised pointers that auto dereference", which is somewhere between wrong and dangerous.
这篇关于为什么默认情况下,lambda函数会丢弃推导的返回类型引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!