在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/