Can't C++ POD type have any constructor?

I have a class and a const variable.

struct A 
{
    int b;
};

A const a;

The class A is POD and can be initialized like this.

A const a = { 3 };

IMHO, it looks fine to have a constructor like this.

struct A 
{
    int b;

    A(int newB) : b(newB)
    {
    }
};

But Clang assumes A as non-aggregate type. Why I can't have constructor like that? Or should I do something else?


I modified question to present my original meaning. I had wrote the struct as class by mistake, and sorry for @Johannes about confusing :)


Solution 1:

POD means Plain Old Data type which by definition cannot have user-defined constructor.

POD is actually an aggregate type (see the next quotation). So what is aggregate? The C++ Standard says in section §8.5.1/1,

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected nonstatic data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).

And section §9/4 from the C++ Standard says,

[....] A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.

From this, its also clear that POD class/struct/union though cannot have user-defined assignment operator and user-defined destructor also.


There are however other types of POD. The section §3.9/10 says,

Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.

Read this FAQ : What is a "POD type"?

Solution 2:

The class A is POD and can be initialized like this

Sorry, that is wrong. Because b is private, the class is not a POD.

But Clang assumes A as non-aggregate type. Why I can't have constructor like that? Or should I do something else?

This is a limitation of C++ as it exists currently. C++0x will not have this limitation anymore. While in C++0x your type is not a POD either, your initialization will work (assuming that you make that constructor public).

(Also, I think a better term for you to use here is "aggregate". The requirement for using { ... } is that your class is an aggregate. It doesn't have to be a POD).

Solution 3:

The other answers describe the POD rules pretty well. If you want to get a similar initialization style to a constructor for a POD you can use a make_-style function, for example:

struct A
{
    int i_;
};

A make_A(int i = 0) 
{
    A a = { i }; 
    return a; 
}

now you can get initialized POD instances like:

A a = make_A();