0

As I understand, the next code should lead to a move constructor call, because the ctor argument is a rvalue reference:

#include <iostream>
#include <vector>

class Foo {
public:
    int a, b;

    Foo(int a, int b) : a(a), b(b) {

    }


    Foo(Foo&& other) noexcept : a(other.a), b(other.b) {
        std::cout << "move" << std::endl;
        other = Foo(a, b);
    }

    Foo(const Foo& other) : a(other.a), b(other.b) {
        std::cout << "copy" << std::endl;
    }

    Foo& operator=(const Foo& other) = default;
};

class Wrapper {
public:
    Foo foo;

    explicit Wrapper(Foo&& foo)
            :foo(foo) { }
};

int main()
{
    Foo foo(14, 88);

    Wrapper wrapper(std::move(foo));

    return 0;
}

Nevertheless, there is this console output:

copy

What do I understand wrongly?

  • What's the point of `other = Foo(a, b)` in the "move" constructor? – Some programmer dude Sep 16 '22 at 12:27
  • Because the second `foo` in `foo(foo)` is an lvalue(as it has a name) and not an rvalue. There are plenty of dupes for this. See dupes: [Why is a named rvalue reference an lvalue expression?](https://stackoverflow.com/questions/56316766/why-is-a-named-rvalue-reference-an-lvalue-expression) and [Why are rvalues references variables not rvalue?](https://stackoverflow.com/questions/32620750/why-are-rvalues-references-variables-not-rvalue) – Jason Sep 16 '22 at 12:28
  • 1
    `:foo(foo)` -> `:foo(std::move(foo))` ([online compiler](https://godbolt.org/z/eEYdWvhfK)). By the rule of thumb, if it has a name, it's lvalue (even if the type of that lvalue is "rvalue reference"). `foo` is a name, so it's an lvalue and you need `std::move` to make it rvalue again. – Yksisarvinen Sep 16 '22 at 12:29
  • 3
    @JasonLiam - "_There are plenty of dupes for this_" - I keep afraid to ask any questions here so that I wouldn't see this your comment ;) – mada Sep 16 '22 at 12:39
  • 1
    @John Haha :) People on SO have to realize that they should first *"search and then research"* as mentioned in [how to ask](https://stackoverflow.com/help/how-to-ask) before asking a question. I admit that i've also asked some questions that were dupes. And I don't mind if someone closes them as dupes. – Jason Sep 16 '22 at 12:41
  • I have also asked questions that turned out to be dupes, and deleted my duplicate question. Often, it's not knowing what terminology to use for the problem at hand, so search-and-research turns up nothing. Or the issue involves symbols that are hard to search for, until one is aware of the jargon the symbol represents. – Eljay Sep 16 '22 at 12:48
  • @Eljay - If you posted them in a way a that most would (because let's face it, the jargon sucks at times), you could have kept them up. Asking a dupe is good, when it can point to non-obvious, jargon laced, canonical. – StoryTeller - Unslander Monica Sep 16 '22 at 13:39
  • @StoryTeller-UnslanderMonica • aye, if they were good as a breadcrumb to refer to the proper Q&A, I'd have kept them. But mine weren't good as a duplicate, they were word salad floundering. (I let all the downvotes help nudge me to delete them.) – Eljay Sep 16 '22 at 13:59

0 Answers0