c++ - Is there a way to emplace return value? -
// version 1 struct range { int begin, end; }; inline range getrange() { int newbegin, newend; // 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; // calculations ranges.emplace_back(newbegin, newend); } }; version 2 faster version 1 . fact is, getrange() used multiple classes. if apply version 2 , there lot of code duplication. also, cannot pass ranges non-const reference getrange() , other classes use std::stack instead of std::vector . have create multiple overloads , have more code duplications. is there common way/idiom emplace return value ? followin...