8
votes

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

  1. new vector constructed from container by moving
  2. container gets destructed

After function return:

  1. tmp is constructed by copy constructor

My question is why isn't tmp constructed by move constructor ?

As I understand things function return type is rvalue and should be moved.

1
Hmm g++ and clang++ (libstdc++ and libc++) do move the vector: coliru.stacked-crooked.com/a/27aacf34fc3e22b3 while VS2013 copies it: rextester.com/JEXDDD72242 - dyp
VS2013 also moves the vector when calling the conversion operator explicitly: rextester.com/SSMH48781 - dyp
VS2015 also moves. Just a bug, I suppose. - T.C.
@dyp When the conversion is implicit VS2013 copy rather than moving - Alejandro Freeman

1 Answers

2
votes

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