Topic: Move semantics for an element of an rvalue array/container?


Author: Niels Dekker - no return address <noreply@this.is.invalid>
Date: Fri, 30 Jan 2009 10:28:50 CST
Raw View
When retrieving an element from a temporary built-in array or temporary
STL container (for example by operator[]), shouldn't it be an rvalue
reference?  In the following example, shouldn't str1 and str2 be
move-constructed, rather than copy-constructed?

    //////////////////////////////////////////////////////////////////////
    #include <vector>
    #include <string>
    using namespace std;

    struct array_holder { string c_array[42]; };
    array_holder get_array();
    vector<string> get_vector();

    int main()
    {
      // Move semantics?
      string str1 = get_vector().at(1);
      string str2 = get_array().c_array[2];  // temporary built-in array
    }
    //////////////////////////////////////////////////////////////////////

As C++0x will provide a means to overload member functions for rvalue
objects, using ref-qualifiers, I would expect a function like
std::vector::at to be overloaded as follows:

    const value_type& at(size_type n) const &;
    value_type& at(size_type n) &;
    value_type&& at(size_type n) &&;

And I would also expect that an element of an rvalue array would itself
be an rvalue.  Does that sound reasonable?  Or has it been considered
before?

Kind regards,

    Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]