Johan Lundberg
Johan Lundberg

Reputation: 27078

lack of compiler warning with incorrect brace initializer

Update2:

As suspected, this does not have to do with having an initializer list constructor. With the comment from R. Martinho Fernandes it's clear that it's just trying to construct an object with a copy of itself that is not detected when using brace syntax:

struct C{
   C(){}   
};
struct D{
   C c0{c0};  // << -- compiles without warning
   C c1(c1);  // << -- does not compile
};

The question remains. Is a diagnostic required by the standard for this case? I do realize diagnostics is not possible or practical for all kinds of errors.

I ended up reporting this, as bug 57758.

Original question:

Someone I know well managed to write erroneous code (caused by pure mistyping) that eventually generated spurious bad_alloc exceptions. I wonder if there is a good reason gcc (4.7.2 and 4.8.1) does not warn about this.

Is a diagnostic required by the standard for this case? I do realize diagnostics is not possible or practical for all kinds of errors.

This is what it boils down to:

#include <initializer_list>
struct A{};
struct C{
   C(std::initializer_list<A*> as){}   
};
struct D{
   C c{c}; // <<- well...
};    
int main(){
   D d;
}

Edit: The reason I mention initializer_list is that if I remove the initializer list constructor I do get an error: error: too many initializers for ‘C’

Which I think is caused by the fact that since there is no user defined (user declared?) constructor, I get aggregate-initialization, which does not work since there is no member (of type C) in C.

Upvotes: 5

Views: 246

Answers (1)

Yktula
Yktula

Reputation: 14849

Clang++ has the same behavior. You are passing the value of c0 when it is uninitialized into the default copy constructor without receiving an "uninitialized when used here" warning. (Write a private copy constructor to confirm.)

I think what is happening is direct-list-initialization. I don't know if a diagnostic is required by the standard, but it's worth filing a bug report about.

Upvotes: 1

Related Questions