如何避免移动副本

时间:2015-09-08 16:47:30

标签: c++ c++11

我需要创建一个带参数的仿函数并在访问器中调用它 - 我希望编译器能够优化代码以提高速度。这是我的尝试:

struct X {
  X(int a, float b) : _a(a), _b(b) {}
  float operator()(float value) const { /* do something */; }
  int _a;
  float _b;
};

struct Y {
  Y(const X&& x) : _x(move(x)) {}
  float operator()(float value) const { return _x(value); }
  const X&& _x;
};

我在类Value的某个Object的访问器中调用它:

float getValue() const {    
  X x(2, 3.14);
  Y y(move(x));
  return y(_value);
}

这是编码此代码的最佳方式,还是可以更简单的方式完成?

0 个答案:

没有答案