0

In a simple example like the following:

struct MyClass {
    MyClass() {}
    MyClass(const MyClass&) {}
};

int main(){
    std::vector<MyClass> objects;
    MyClass obj1;
    objects.push_back(obj1);
    for (auto&& obj: object) {
    }
    return 0;
}

how does the auto&& work? The class doesn't have any move support, what is this code supposed to do? I don't understand..

Dean
  • 5,226
  • 1
  • 24
  • 71

1 Answers1

1

The type is deduced from the type obtained by dereferencing the sequence's iterator type; that is an lvalue reference, MyClass&. By the "reference collapsing" rules, trying to deduce the type "rvalue reference to lvalue reference" gives the lvalue reference type.

So auto&& is deduced to be MyClass&.

If the sequence had an iterator type that gave rvalue references, then it would instead be deduced as MyClass&&, enabling move semantics if the class supports them.

Mike Seymour
  • 235,407
  • 25
  • 414
  • 617