// VERSION 1
struct Range { int begin, end; };
inline Range getRange()
{
int newBegin, newEnd;
// do calculations
return {newBegin, newEnd};
}
struct Test
{
std::vector<Range> ranges;
inline void intensive()
{
ranges.push_back(getRange());
// or ranges.emplace_back(getRange());
// (gives same performance results)
}
};
// VERSION 2
struct Range { int begin, end; };
struct Test
{
std::vector<Range> ranges;
inline void intensive()
{
int newBegin, newEnd;
// do calculations
ranges.emplace_back(newBegin, newEnd);
}
};
バージョン 2は常にバージョン 1より高速です。
実際には、getRange()
複数のクラスで使用されています。バージョン 2を適用すると、多くのコードの重複が発生します。
また、一部のクラスでは の代わりに を使用するため、ranges
への非 const 参照として渡すことはできません。複数のオーバーロードを作成し、コードの重複を増やす必要があります。getRange()
std::stack
std::vector
戻り値を配置する一般的な方法/イディオムはありますか?