templateに関するwkwkhautboisのブックマーク (1)

  • Expression Template - Faith and Brave - C++で遊ぼう

    operator+ - * / のような演算子の一般的な実装では一時オブジェクトのコストが発生します。 Vector operator+(const Vector& l, const Vector& r) { const std::size_t n = l.size(); Vector tmp(l); for (std::size_t i = 0; i < n; ++i) tmp[i] += r[i]; return tmp; } そのため、以下のような式を書いた場合 Vector x, y, z, t; t = x + y + z; x + y + z では 2 つの一時オブジェクトが生成されてしまうのです。 以下のように書けば一時オブジェクトは生成されませんが const std::size_t n = x.size(); for (std::size_t i = 0; i < n;

    Expression Template - Faith and Brave - C++で遊ぼう
  • 1