3

Consider the following code:

#include <initializer_list>

class C {
public:
    C() = delete;
    C(int) {}
};

class D {
public:
    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({});                       // still fine
    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?

Update

An issue on page 55 in Scott Meyer's "Effective Modern C++" made me think that having empty braces in a braced initialization would call the initializer_list constructor with an empty list. That is wrong. For details, see this blog post from the author.

1

1 Answer 1

5

D d5{{}}; attempts to initialize d5 with a one-element initializer list. That one element is {} which is a shorthand for C{} - a default-constructed instance of C. But C doesn't have a default constructor - hence the error.

Sign up to request clarification or add additional context in comments.

4 Comments

Ahh, you mean D d5{{}} is the same as D d5{C{}}?
@sergej Yes. It's easy to demonstrate experimentally - just modify the program to have C() constructor print something, and/or have D's list constructor print the size of the list.
@IgorTandetnik how were you able to diagnose this issue? did you add couts to the ctorsas mentioned in your comments?
@PYA No, just from the error message and first principles. I haven't compiled the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.