应该std :: bind需要移动构造函数吗?

时间:2018-03-27 08:48:24

标签: c++ gcc c++builder stdbind

当我在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();
}

在代码中,我在类A中定义析构函数,以便不隐式定义移动构造函数。该类仍应具有隐式定义的默认构造函数和复制构造函数。 Documentation says&#34;要复制或移动绑定的参数&#34;,所以我希望有一个复制构造函数足以使用bind。

两个编译器之间的区别是什么?它是这个实现定义的行为还是我在这里错误地使用std :: bind?

C ++ Builder正在使用C ++ 11而Ideone是C ++ 14,这可以解释其中的区别吗?

1 个答案:

答案 0 :(得分:0)

std :: bind应将所有CopyConstructible参数复制到其返回值中:

  

std :: bind的返回类型包含从std :: forward(f)构造的std :: decay :: type类型的成员对象,以及每个args的一个对象...,类型为std :: decay :: type,类似地从std :: forward(arg_i)构造。

在此测试用例中,不必调用移动构造函数。这看起来像是C ++ Builder中的一个错误。我已提交错误报告:https://quality.embarcadero.com/browse/RSP-20209