Saturday, January 23, 2010

const variable requires initializer / no explicitly declared default constructor

I recently discover a subbtle bug in gcc. Consider the following:

struct A
{
};

int main()
{
const A a;
return 0;
}

It properly fails as expected:

t.cxx: In function ‘int main()’:
t.cxx:7: error: uninitialized const ‘a’


However now consider this one:

struct A
{
A(){}
};

struct B : public A
{
};

int main()
{
const B b;
return 0;
}


It is compiling without warning with

/usr/lib/gcc-snapshot/bin/g++ -pedantic -W -Wall -Wextra t2.cxx

Using:

/usr/lib/gcc-snapshot/bin/g++ --version
g++ (Debian 20100117-1) 4.5.0 20100117 (experimental) [trunk revision 155979]

This is logged as:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42844

No comments: