- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是明确定义的行为吗?
#include <functional>
void foo() {
auto f = new std::function<void()>;
*f = [f]() { delete f; };
(*f)();
f = nullptr;
}
int main() {
foo();
}
使用最新的 g++,如果我在模板中执行此操作,则在 valgrind 下运行时会导致无效读取,否则它会正常工作。为什么?这是 g++ 中的错误吗?
#include <functional>
template<std::size_t>
void foo() {
auto f = new std::function<void()>;
*f = [f]() { delete f; };
(*f)();
f = nullptr;
}
int main() {
foo<0>();
}
最佳答案
此程序具有明确定义的行为并演示了 g++ 错误。
运行时唯一有问题的部分是在语句 (*f)();
期间.该行的行为可以一段一段地分开。以下标准部分编号来自 N3485;如果有些与 C++11 不匹配,我们深表歉意。
*f
只是指向类类型的原始指针的内置一元运算符。这里没问题。唯一的其他评估是函数调用表达式 (*f)()
,调用 void std::function<void()>::operator() const
.那么那个完整的表达式就是一个丢弃的值。
20.8.11.2.4:
R operator()(ArgTypes... args) const
Effects:
INVOKE
(obj, std::forward<ArgTypes>(args)..., R)
whereobj
is the target object of*this
.
(我已将标准中的“f
”替换为“obj
”,以减少与 main
的 f
的混淆。)
在这里obj
是 lambda 对象的拷贝,ArgTypes
是来自特化的空参数包 std::function<void()>
, 和 R
是void
.
INVOKE
伪宏在 20.8.2 中定义。自类型 obj
不是指向成员的指针, INVOKE
(obj, void)
定义为 obj()
隐式转换为 void
.
5.1.2p5:
The closure type for a lambda-expression has a public
inline
function call operator ...
... 带有准确描述的声明。在这种情况下,结果是 void operator() const
.并且它的定义也被准确描述:
5.1.2p7:
The lambda-expression's compound-statement yields the function-body of the function call operator, but for purposes of name lookup, determining the type and value of
this
and transforming id-expressions referring to non-static class members into class member access expressions using(*this)
, the compound-statement is considered in the context of the lambda-expression.
5.1.2p14:
For each entity captured by copy, an unnamed non-static data member is declared in the closure type.
5.1.2p17:
Every id-expression that is an odr-use of an entity captured by copy is transformed into an access to the corresponding unnamed data member of the closure type.
因此 lambda 函数调用运算符必须等效于:
void __lambda_type::operator() const {
delete __unnamed_member_f;
}
(我为未命名的 lambda 类型和未命名的数据成员发明了一些名称。)
该调用运算符的单个语句当然等同于 delete (*this).__unnamed_member_f;
所以我们有:
operator*
取消引用(在纯右值 this
上)delete
表达
std::function<void()>::~function()
void operator delete(void*)
最后,在 5.3.5p4 中:
The cast-expression in a delete-expression shall be evaluated exactly once.
(这里是 g++ 错误的地方,在析构函数调用和释放函数之间对成员子对象进行第二次值计算。)
此代码不会在 delete
之后导致任何其他值计算或副作用表达。
在 lambda 类型和 lambda 对象中允许实现定义的行为,但不会影响以上任何内容:
5.1.2p3:
An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing:
the size and/or alignment of the closure type,
whether the closure type is trivially copyable,
whether the closure type is a standard-layout class, or
whether the closure type is a POD class.
关于c++ - 删除自身内的 std::function 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998364/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!