I understand that when objects are returned by-value from a function, their copy-constructors are called. If a class has a deleted copy-constructor, returning by value will fail.
struct X {
X(const X &) = delete;
};
X f() {
return X{};
}
error: call to deleted constructor of 'X'
C++11 gives us extended-initializers. And I read somewhere on a SO post that this
X f() {
return {};
}
is the same as
X f() {
return X{};
}
So why doesn't the below code give me an error? It passes and I even get to call the function in main:
struct D {
D(const D &) = delete;
};
D f() { return {}; }
int main()
{
f();
}
Here is a demo. No error is reported. I find that weird because I believe that the copy-constructor should be called. Can anyone explain why no error is given?