so i have a flyweight of strings type:
typedef boost::flyweight< std::string, boost::flyweights::intermodule_holder > SymbolName_t;
and i want to push a instance of this into a vector of them, but the naive approach won't work:
void PushSome( std::vector < SymbolName_t >& list)
{
std::string& str = getSomeStr();
list.push_back( str ); // <--- won't compile
}
so i added a temporary constructor:
void PushSome( std::vector < SymbolName_t >& list)
{
std::string& str = getSomeStr();
list.push_back( SymbolName_t(str) ); // <--- compiles ok
}
my question is: is this approach optimal, given the constraints of the language? what benefits would provide implementing this in some other way, say, by providing a static conversion operator? I don't consider a implicit conversion via a non-explicit constructor a valid option because that would require modifying the boost::flyweight
template
If you have a C++11 compiler, you can use emplace_back
instead of push_back
, which eliminates the need for the copy.