Search code examples
c++move-semanticsrvalue-referencemove-constructormost-vexing-parse

Confusion with move constructors: unable to call move constructor


I've been having difficulties understanding move constructors in C++. I have made a simple class with a default constructor, copy constructor, move constructor and destructor. Also, I have defined a function with two overloads, one accepts a reference to that class and one accepts an rvalue reference to that class. My test code is below.

#include <iostream>


class c {

public:

    c() {
        std::cout << "default constructor" << std::endl;
    }

    c(const c& s) {
        std::cout << "copy constructor" << std::endl;
    }

    c(c&& s) {
        std::cout << "move constructor" << std::endl;
    }

    ~c() {
        std::cout << "destructor" << std::endl;
    }

};

void f(c& s) {
    std::cout << "passed by reference" << std::endl;
}

void f(c&& s) {
    std::cout << "passed by rvalue reference" << std::endl;
}

int main() {

    c s1; // line 1
    std::cout << "\n";
    c s2(s1); // line 2
    std::cout << "\n";
    c s3(c()); // line 3

    std::cout << "\n";

    f(s1); // line 4
    std::cout << "\n";
    f(c()); // line 5

    getchar();
    return 0;

}

The output I'm getting is not what I was expecting. Below is the output I'm getting from this code.

default constructor

copy constructor

passed by reference

default constructor
passed by rvalue reference
destructor

I can understand the outputs from all lines except line 3. On line 3, which is c s3(c());, c() is an rvalue so I'd expect that s3 would be move constructed. But the output doesn't show that it is move constructed. On line 5, I'm doing the same thing and passing an rvalue to the function f() and it indeed calls the overload that accepts an rvalue reference. I'm very confused and would appreciate any information on this.

Edit: I am able to call the move constructor if I do c s3(std::move(c())); but am I not already passing an rvalue to s3? Why would I need std::move?


Solution

  • The reason why you don't see any output from line 3 is that it declares a function, not a variable. This is due to an ambiguity called Most Vexing Parse.

    Compare c s3(c()) with int foo(int ()), which, thanks to implicit type adjustments, is the same as int foo(int (*f)()).

    To get around this, use brace initialisation (which was actually introduced in C++11 partly for this very reason):

    c s3(c{});
    
    // or
    
    c s3{c()};
    
    // or
    
    c s3{c{}};