- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已阅读 the SO question here并理解了这部分答案:“但是如果你将一个临时变量绑定(bind)到一个非常量引用,你可以“永远”传递它只是为了让你对对象的操作消失,因为在这个过程中的某个地方你完全忘记了这个是暂时的。”
也就是说,在下面:
#include <iostream>
void modifyValue(int& rValue) {
rValue++;
}
int main() {
modifyValue(9899);
return 0;
}
如果右值可以绑定(bind)到非常量左值引用,那么可能会进行许多最终会被丢弃的修改(因为右值是临时的),这是无用的。
然而,这似乎定义明确(写入临时值就像写入任何值一样,生命周期与写入的有效性无关)。
这是禁止指定绑定(bind)的一个完全正确的理由(即使绑定(bind)会被明确定义),但是一旦我认为禁止这种绑定(bind)会强制需要转发引用,我的问题就开始形成了。
关于为什么右值不能绑定(bind)到非常量左值引用,是否还有任何其他原因(即除了写入临时值之外)?
最佳答案
简单的答案是,在大多数情况下,将临时变量传递给需要可变左值引用的函数表示逻辑错误,而 c++ 语言正在执行它最好能帮助您避免犯错误。
函数声明:void foo(Bar& b)
暗示了以下叙述:
foo takes a reference to a Bar,
b
, which it will modify.b
is therefore both an input and an output
传递临时变量作为输出占位符通常比调用返回对象的函数更严重的逻辑错误,只是为了丢弃未经检查的对象。
例如:
Bar foo();
void test()
{
/*auto x =*/ foo(); // probable logic error - discarding return value unexamined
}
但是,在这两个版本中,都没有问题:
void foo(Bar&& b)
foo takes ownership of the object referenced by Bar
void foo(Bar b)
foo conceptually takes a copy of a Bar, although in many cases the compiler will decide that creating and copying a Bar is un-necessary.
所以问题是,我们要达到什么目的?如果我们只需要一个 Bar 来工作,我们可以使用 Bar&& b
或 Bar b
版本。
如果我们想可能使用一个临时的并且可能使用一个现有的Bar,那么我们可能需要foo
的两个重载>,因为它们在语义上会略有不同:
void foo(Bar& b); // I will modify the object referenced by b
void foo(Bar&& b); // I will *steal* the object referenced by b
void foo(Bar b); // I will copy your Bar and use mine, thanks
如果我们需要这种可选性,我们可以通过将一个包裹在另一个中来创建它:
void foo(Bar& b)
{
auto x = consult_some_value_in(b);
auto y = from_some_other_source();
modify_in_some_way(b, x * y);
}
void foo(Bar&& b)
{
// at this point, the caller has lost interest in b, because he passed
// an rvalue-reference. And you can't do that by accident.
// rvalues always decay into lvalues when named
// so here we're calling foo(Bar&)
foo(b);
// b is about to be 'discarded' or destroyed, depending on what happened at the call site
// so we should at least use it first
std::cout << "the result is: " << b.to_string() << std::endl;
}
有了这些定义,这些现在都是合法的:
void test()
{
Bar b;
foo(b); // call foo(Bar&)
foo(Bar()); // call foo(Bar&&)
foo(std::move(b)); // call foo(Bar&&)
// at which point we know that since we moved b, we should only assign to it
// or leave it alone.
}
好的,为什么这么关心?为什么在无意义的情况下修改临时文件会是逻辑错误?
好吧,想象一下:
Bar& foo(Bar& b)
{
modify(b);
return b;
}
我们期望做这样的事情:
extern void baz(Bar& b);
Bar b;
baz(foo(b));
现在想象这可以编译:
auto& br = foo(Bar());
baz(br); // BOOM! br is now a dangling reference. The Bar no longer exists
因为我们被迫在 foo
的特殊重载中正确处理临时文件,foo
的作者可以确信这个错误永远不会在您的代码中发生。
关于c++ - 为什么右值不能绑定(bind)到非 const 左值引用,除了写入临时值无效这一事实之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51836609/
我不知道该怎么做... function f1() { var x = 10; function f2(fx) { var x; x = 6;
早期绑定(bind)和后期绑定(bind)有什么区别? 最佳答案 简短的回答是,早期(或静态)绑定(bind)是指编译时绑定(bind),后期(或动态)绑定(bind)是指运行时绑定(bind)(例如
如何在 SwiftUI View 上使用 Binding(get: { }, set: { }) 自定义绑定(bind)与 @Binding 属性。我已成功使用此自定义绑定(bind)与 @State
我经常发现自己遇到问题,即控件的两个(相关)值被更新,并且两者都会触发昂贵的操作,或者控件可能会暂时处于不一致的状态。 例如,考虑一个数据绑定(bind),其中两个值 (x,y) 相互减去,最终结果用
我想通过我的 ViewModel 控制我的一个窗口的高度和宽度。 这看起来很简单。 但没有。它不起作用。 它检查 ViewModel 的 Width但不是 Height . 奇怪的是,如果我切换 W
UI5中一次性绑定(bind)和单向绑定(bind)有什么区别? 是否有任何用户特定的用例我会使用它们? 我无法从文档中获得太多信息。 最佳答案 单程 它的作用:单向数据流。模型数据的变化(例如通过
(define make (lambda (x) (lambda (y) (cons x (list y))))) (let ((x 7) (p (make 4))) (cons
尽管我或多或少地了解什么是语言绑定(bind),但我很难理解它们是如何工作的。 例如,谁能解释一下如何为 WinAPI 制作 Java 绑定(bind)? 最佳答案 如果您搜索 Foreign Fun
谁能解释为什么我可以重新绑定(bind)列表但不能+? (binding [list vector] (list 1 3)) (binding [list +] (list 1 3)) (bi
我真的很喜欢 Caliburn 和命名约定绑定(bind),我很惊讶 可见性与“CanNAME”约定用于保护 Action 的方式不同。 据我所知, BooleanToVisibilityConver
我了解动态绑定(bind)的实现方式以及静态绑定(bind)和动态绑定(bind)之间的区别,但我只是无法理解动态绑定(bind)的定义。基本上它是一种运行时绑定(bind)类型。 最佳答案 基本上,
http://jsfiddle.net/3NRsd/ var foo = $("div").bind("click", function() { $("div").animate({"hei
这个问题我快疯了...我有一个用户控件,它有一个用于插入操作的 FormView 和一个用于所有其他操作的 GridView。 在这两个控件中,我都有一个 DropDownList,如下所示: '
我有一个绑定(bind)到 ListBox 的地址的 ObservableCollection。然后在 ItemTemplate 中,我使用 {Binding .} 绑定(bind)到当前地址记录。这
如果我有以下简单的 js/knockout 代码: .js( View 模型): var image = ko.observable('http://placehold.it/300x150'); 看
我正在 aurelia 上开发一个自定义属性,让用户在输入文本区域时从列表中进行选择。例如,用法将是这样的: 正如您可能注意到的,auto-complete是属性。现在,当我想显示提示时,我想在自定
我正在使用 EventEmitter2作为我的应用程序内部的消息总线。现在我需要绑定(bind)和取消绑定(bind)一些事件处理程序。因为我也希望他们bind将它们添加到给定的上下文中,我最终得到以
我有以下函数调用: $(".selector").on("click", callback.bind(this, param1, param2)); 在我的回调函数中,我想使用绑定(bind)的 th
我目前正在试验新的编译绑定(bind),并且(再次)达到了我在拼图中遗漏了一个小问题:为什么我必须调用 Bindings.Update?直到现在,我还认为实现 INotifyPropertyChang
我正在阅读一本关于编写 JavaScript 框架的书,并找到了这段代码。但是我不明白它是如何工作的,尤其是 bind.bind 的用法?有人知道吗? var bind = Function.prot
我是一名优秀的程序员,十分优秀!