Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between ctor{} and {} when returning non-movable, non-copyable object

Tags:

c++

c++11

Here is the situation I came up with:

#include <iostream>
using namespace std;

struct test {
    test() { cout << "ctor" << endl; }

    test(const test&) = delete;
    test(test&&)      = delete;
};

auto f() -> test {
    return {};
    // return test{};
}

auto main() -> int {
    f();
}

This code compiles with both clang and gcc, but when I change return {} to return test{} it doesn't compile anymore. Why is that? Shouldn't it work the same in both cases? Frankly, I don't know if there is a good use case for this, but it caught me by surprise, so now I'm wondering what's going on.

like image 672
catscradle Avatar asked Nov 06 '13 23:11

catscradle


1 Answers

return {} uses an empty initialiser list to initialise the return value, using the default constructor.

return test{} creates a temporary using the default constructor, then uses that to initialise the return value using a move or copy constructor. You have deleted those constructors, so that can't be done.

In practice, the copy or move will be elided so that both have the same effect - but the second still requires an accessible constructor, even if it's not actually used.

like image 122
Mike Seymour Avatar answered Oct 19 '22 04:10

Mike Seymour