std::string Concatenate(const std::string& s1,
const std::string& s2,
const std::string& s3,
const std::string& s4,
const std::string& s5)
{
return s1 + s2 + s3 + s4 + s5;
}
デフォルトでreturn s1 + s2 + s3 + s4 + s5;
は、次のコードと同等である可能性があります。
auto t1 = s1 + s2; // Allocation 1
auto t2 = t1 + s3; // Allocation 2
auto t3 = t2 + s4; // Allocation 3
return t3 + s5; // Allocation 4
割り当て時間を 1 に短縮するエレガントな方法はありますか? そのままにしておくということreturn s1 + s2 + s3 + s4 + s5;
ですが、効率は自動的に改善されます。可能であれば、プログラマーによる の誤用を防ぐこともできますstd::string::operator +
。
ref-qualifierメンバー関数は役に立ちますか?