小编mmo*_*rny的帖子

将局部变量绑定到闭包的最便宜的方法

我相信以下是将局部变量绑定到闭包的最便宜的方法:

void ByRValueReference(A&& a) {
}

std::function<void ()> CreateClosureByRValueReference() {
  A a;
  std::function<void ()> f = std::bind(&ByRValueReference, std::move(a)); // !!!
  return f;
}
Run Code Online (Sandbox Code Playgroud)

但是,它不能在Clang 3.1下编译:

error: no viable conversion from '__bind<void (*)(A &&), A>' to 'std::function<void ()>'
Run Code Online (Sandbox Code Playgroud)

和gcc 4.6.1:

/usr/include/c++/4.6/functional:1778:2: error: no match for call to ‘(std::_Bind<void (*(A))(A&&)>) ()’
Run Code Online (Sandbox Code Playgroud)

我违反了标准,还是只是破坏了标准库?

c++ c++11

5
推荐指数
1
解决办法
564
查看次数

标签 统计

c++ ×1

c++11 ×1