为什么 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
被愉快地初始化时难道不是一个错误吗?一个返回非引用的函数?我也通过明确的写作看到了这一点:
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 行中使用表达式
.c.getObj()
.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. 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屋!