std::bind 是否需要移动构造函数?
Should std::bind require move constructor?
当我在 GCC 6.3 (ideone.com) 中尝试以下代码时,它会编译并打印 "OK!"。当我在 C++ Builder 10.1 中尝试相同的代码时,编译失败:
[bcc32c Error] tuple(110): no matching constructor for initialization of 'A'
tuple(433): in instantiation of function template specialization 'std::_Tuple_val<A>::_Tuple_val<std::_Tuple_val<A> >' requested here
File3.cpp(4): candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::_Tuple_val<A>' to 'const A' for 1st argument
File3.cpp(4): candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
第 433 行是 tuple(_Myt&&) = default;
。
#include <iostream>
#include <functional>
struct A {
// Define destructor to delete default move constructor
~A() {}
int a = 0;
};
void func(const A&)
{
std::cout << "OK!" << std::endl;
}
int main() {
A a;
auto f = std::bind(&func, a);
f();
}
在代码中,我在class A 中定义了析构函数,因此没有隐式定义移动构造函数。 class 仍应隐式定义默认构造函数和复制构造函数。 Documentation says "The arguments to bind are copied or moved",所以我希望有一个复制构造函数就足以使用绑定。
如何解释两种编译器之间的差异?这是实现定义的行为还是我在这里使用 std::bind 不正确?
C++ Builder 使用的是 C++11,而 Ideone 使用的是 C++14,这可以解释其中的区别吗?
std::bind 应将所有 CopyConstructible 参数复制到其 return 值中:
The return type of std::bind holds a member object of type std::decay::type constructed from std::forward(f), and one object per each of args..., of type std::decay::type, similarly constructed from std::forward(arg_i).
在这个测试用例中,应该不需要调用移动构造函数。这看起来像是 C++ Builder 中的错误。我已提交错误报告:https://quality.embarcadero.com/browse/RSP-20209
当我在 GCC 6.3 (ideone.com) 中尝试以下代码时,它会编译并打印 "OK!"。当我在 C++ Builder 10.1 中尝试相同的代码时,编译失败:
[bcc32c Error] tuple(110): no matching constructor for initialization of 'A'
tuple(433): in instantiation of function template specialization 'std::_Tuple_val<A>::_Tuple_val<std::_Tuple_val<A> >' requested here
File3.cpp(4): candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::_Tuple_val<A>' to 'const A' for 1st argument
File3.cpp(4): candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided
第 433 行是 tuple(_Myt&&) = default;
。
#include <iostream>
#include <functional>
struct A {
// Define destructor to delete default move constructor
~A() {}
int a = 0;
};
void func(const A&)
{
std::cout << "OK!" << std::endl;
}
int main() {
A a;
auto f = std::bind(&func, a);
f();
}
在代码中,我在class A 中定义了析构函数,因此没有隐式定义移动构造函数。 class 仍应隐式定义默认构造函数和复制构造函数。 Documentation says "The arguments to bind are copied or moved",所以我希望有一个复制构造函数就足以使用绑定。
如何解释两种编译器之间的差异?这是实现定义的行为还是我在这里使用 std::bind 不正确?
C++ Builder 使用的是 C++11,而 Ideone 使用的是 C++14,这可以解释其中的区别吗?
std::bind 应将所有 CopyConstructible 参数复制到其 return 值中:
The return type of std::bind holds a member object of type std::decay::type constructed from std::forward(f), and one object per each of args..., of type std::decay::type, similarly constructed from std::forward(arg_i).
在这个测试用例中,应该不需要调用移动构造函数。这看起来像是 C++ Builder 中的错误。我已提交错误报告:https://quality.embarcadero.com/browse/RSP-20209