c++ - 是否可以在不构造的情况下将 std::bind 的返回值实际转换为 std::function?

标签 c++ c++11 casting stl

this question ,OP 声称 main 函数中的第三行将 std::bind 的返回值转换为 std::function

但是,它看起来像是对我的简单构造函数调用。

是否有一种合法的方法可以在不构造新对象的情况下将std::bind 的返回值转换std::function

或者,将 std::bind 的返回值地址转换为 std::function* 然后调用通过引用它?

假设我知道std::function 的适当模板参数。也就是说,假设它们与我们从 std::bind 的返回值构造 std::function 实例时使用的模板参数相同。

最佳答案

标准将此称为带有函数符号的显式类型转换。本质上,它用于构造指定类型的值。虽然看起来像是构造函数调用,但实际上在 C++ 中没有办法直接调用构造函数。

Is there a legitimate way to actually cast the return value of std::bind to std::function without constructing a new object?

没有。 std::bind 的返回类型未指定;只要它提供正确的操作和语义,它就可以返回任何东西。您无法以可移植的方式将其视为 std::function

Alternately, is it kosher (not undefined behavior) to cast the the address of a return value of std::bind to std::function* and then invoke it by deferencing it?

不,那是未定义的行为。

关于c++ - 是否可以在不构造的情况下将 std::bind 的返回值实际转换为 std::function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248844/

相关文章:

c++ - 代码块总是在项目中运行 main.cpp

c++ - 如何从 C++ 文件中读取由逗号分隔的全名?

c++ - OpenGL 计算法线(四边形)

C++11 析构函数 = 删除

java - byte[] 到 String 和 String 到 byte[]

c++ - 为什么我需要转换算法 remove() 的参数?

C++ INET 菜鸟,段错误

c++ - 如何保证 const static 成员的初始化顺序?

c++ - 将两个字符的字符串变量转换为十六进制

vb.net - 从 System.Linq.EnumerableQuery 转换为 System.Data.Datatable(使用动态 LINQ)