相关疑难解决方法(0)

何时应该在函数返回值上使用std :: move?

在这种情况下

struct Foo {};
Foo meh() {
  return std::move(Foo());
}
Run Code Online (Sandbox Code Playgroud)

我很确定移动是不必要的,因为新创建的Foo将是一个xvalue.

但在这种情况下呢?

struct Foo {};
Foo meh() {
  Foo foo;
  //do something, but knowing that foo can safely be disposed of
  //but does the compiler necessarily know it?
  //we may have references/pointers to foo. how could the compiler know?
  return std::move(foo); //so here the move is needed, right?
}
Run Code Online (Sandbox Code Playgroud)

我认为需要采取行动吗?

c++ move-semantics c++11

109
推荐指数
4
解决办法
5万
查看次数

std :: is_assignable和std :: pair <const T,U>

正如所料,以下代码无法编译.

#include <type_traits>
#include <utility>
int main()
{
    using T = std::pair<const int, int>;
    const auto ok = std::is_assignable<T, T>::value; // true
    T x;
    T y;
    x = y; // compiler error
}
Run Code Online (Sandbox Code Playgroud)

但是以下三个编译器的值ok 是正确的.

  • g ++(Ubuntu 5.4.0-6ubuntu1~16.04.4)5.4.0 20160609
  • clang版本3.8.0-2ubuntu4(标签/ RELEASE_380/final)
  • MSVC++ 2017 15.2 26430.6

为什么是这样?

c++ c++11

3
推荐指数
1
解决办法
162
查看次数

标签 统计

c++ ×2

c++11 ×2

move-semantics ×1