Consider the following code:
#include <initializer_list>
class C {
public:
C() = delete;
C(int) {}
};
class D {
public:
D() = delete;
D(std::initializer_list<C> il) {}
};
int main()
{
std::initializer_list<C> il{}; // fine: empty list, no need to construct C
D d2(il); // fine: calls initializer_list ctor with empty list
D d3{il}; // ditto
D d4({}); // ditto
D d5; // error: use of deleted function 'C::C()'
// WHY is the constructor of 'C' required here?
}
I thought D d5;
would call the initializer_list
constructor of D
with an empty list. And, as the list is empty, the constructor of C
would not be called. However, it does not compile:
error: use of deleted function
'C::C()'
--D d5;
What is the rationale behind this error?
Aucun commentaire:
Enregistrer un commentaire