Consider the following code:
class StringTokenizer
{
private:
char m_delimiter;
std::istringstream m_string;
public:
explicit StringTokenizer(const std::string& str, char delimiter)
: m_string(str)
, m_delimiter(delimiter)
{
}
template <class Container>
operator Container ()
{
Container container;
for (std::string token; std::getline(m_string, token, m_delimiter); )
{
container.insert(container.end(), token);
}
return container;
}
};
This is the usage:
vector<string> tmp = StringTokenizer("123 456", ' '); //Please note the implicit conversion
When debugging the following happens (Using VS2013):
At the return
statement of conversion operator
container
by movingcontainer
gets destructedAfter function return:
tmp
is constructed by copy constructorMy question is why isn't tmp
constructed by move constructor ?
As I understand things function return type is rvalue and should be moved.
VS2013 doesn't automatically generate the move constructor/assignment. This is solved in later versions.
https://msdn.microsoft.com/en-us/library/hh567368.aspx#rvref
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With