7
4

4 回答 4

6

Say:

Test::Dummy const Test::dummy = { };
于 2012-05-31T13:21:01.710 回答
2

See http://gcc.gnu.org/wiki/VerboseDiagnostics#uninitialized_const (which gives the relevant reference to the standard) and also the GCC 4.6 release notes which say

In 4.6.0 and 4.6.1 G++ no longer allows objects of const-qualified type to be default initialized unless the type has a user-declared default constructor. In 4.6.2 G++ implements the proposed resolution of DR 253, so default initialization is allowed if it initializes all subobjects. Code that fails to compile can be fixed by providing an initializer e.g.

struct A { A(); };
struct B : A { int i; };
const B b = B();

Use -fpermissive to allow the old, non-conforming behaviour.

于 2012-05-31T21:42:57.280 回答
1

you could also add a default ctor to class Dummy:

class Dummy { public: Dummy(){} };

in line 4.

EDIT: It appears that gcc 4.4 fails to generate the default ctor for class Dummy. Thus the above overcomes this compiler bug directly.

于 2012-05-31T13:33:00.260 回答
0

With gcc 4.4, use

Test::Dummy const Test::dummy = Test::Dummy;

With compilers supporting C++11, you can use uniform initialization syntax:

Test::Dummy const Test::dummy = { };

But I don't think that's supported by gcc 4.4.

于 2012-05-31T13:36:06.760 回答