std :: function不是gcc中的MoveConstructible函数对象

iw.*_*hin 16 c++ gcc c++11

我想要相处std::function.从这里引用可以看出,对std::functionctor的论证应该是可调用的并且可以复制构造.所以这是一个小例子:

#include <iostream>
#include <type_traits>
#include <functional>

class A {
public:
    A(int a = 0): a_(a) {}
    A(const A& rhs): a_(rhs.a_) {}
    A(A&& rhs) = delete;
    void operator() ()
    {
        std::cout << a_ << std::endl;
    }

private:
    int a_;
};

typedef std::function<void()> Function;

int main(int argc, char *argv[])
{
    std::cout << std::boolalpha;
    std::cout << "Copy constructible: "
              << std::is_copy_constructible<A>::value << std::endl;
    std::cout << "Move constructible: "
              << std::is_move_constructible<A>::value << std::endl;
    //Function f = A();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我们有可调用的,可复制的构造,但不能移动可构造的类.我认为这应该足以包装它Function.但是如果你取消评论注释行编译器就会对删除的移动构造函数感到非常沮丧.这是ideone链接.GCC 4.8.0也不编译.

那么,这是我误解的事情std::function还是GCC的错误行为?

ron*_*nag 11

GCC和Clang是正确的.

§17.6.3.1.1 模板参数要求[utility.arg.requirements]

表20 - MoveConstructible要求[moveconstructible].

  • T u = rv; u等于施工前rv的值.
  • T(RV); T(rv)等于构造前的rv值.

表21 - CopyConstructible要求(除了MoveConstructible)[copyconstructible].

  • T u = v; v的值不变,相当于u.
  • 电视); v的值不变,等于T(v).

注意:

CopyConstructible要求(除了MoveConstructible)

即如果某些东西是CopyConstructible它也必须是MoveConstructible.虽然将移动作为副本实施是好的.

更新:

虽然我觉得有趣的是C++ 11标准似乎没有is_copy_constructible根据CopyConstructible来定义,即它们不完全相同,is_copy_constructible但它更加轻松,因为它只需要:

§20.9.4.3 类型属性[meta.unary.prop]

表49 - 类型属性谓词

  • is_copy_constructible <T>; is_constructible <T,const T&> :: value为true.