D
dragan
James said:Victor said:dragan wrote:
Can C++0x PODs/aggregates have user-defined constructors
and still be PODs/aggregates?
From [dcl.init.aggr]:
<< 1 An aggregate is an array or a class (Clause 9) with no
user-provided constructors (12.1), no private or
protected non-static data members (Clause 11), no base classes
(Clause 10), and no virtual functions (10.3). >>
So, no, once you add a user-defined c-tor, it's not an
aggregate.I'm not sure what aggregates are good for.
Initialization syntax. The most important use is for arrays,
since you can let the compiler count the members when using
aggregate initialization, e.g.:
std::string const names[] = { "Abert", "Bertha", "Charles" ... } ;
Ah, OK. See, an example is so useful. When I see "aggregate", I will now
think "array" AND initialization.
It's also useful for structs, however, when you need static
initialization (especially for tables of structs), e.g.:
struct MapInit
{
char const* key;
int value;
};
MapInit const initTable[] =
{
{ "toto", 42 },
{ "titi", 0 },
// ...
};
I think "aggregate" gets thrown around so much, but it should probably be
reserved more for implementer-level discussion rather than user-level. It
seems so minor relative to the notion of POD (or layout compatibility!
Errgh, so frustrating!).
Layout compatibility is an awkward problem. C++ can mandate a
certain number of things with regards to how the C++ compiler
lays out data, but it can't mandate anything with regards to how
other languages layout data. In the end, it's a lot like the
``extern "C"'' fiasco: C++ requires a compiler to support it,
stating that functions declared ``extern "C"'' must be callable
from C (or something along those lines). Which is all nice and
fine, but what does it mean if the platform doesn't have a C
compiler? Or if it has several C compilers, with different
calling conventions?
"extern C" fiasco? News to me that it was a fiasco (you must be an
implementor/insider, I bet)! I thought it was more like "thank god we can
still use DLLs in C++".
In practice, of course, on most small and medium sized systems
today, the system ABI is defined in terms of C, which means that
there will be a C compiler, and all C compilers will use a
commun layout and common calling conventions,
That does seem to be "the least common denominator". Or at least "one of
them" things.
so the problem
doesn't come up; C is the lowest common denominator.
I really did write the above before I read you saying it (I feel proud of
myself! ).
If you
look at it closely, you'll see that the standard doesn't
actually give any guarantees with regards to standard layout and
other languages, for the reason stated above.
'Care to restate that reason?
There may be some
advanced programming techniques which depend on standard layout,
but for the everyday user, the only really significant
distinction is whether the class could be written in C or
not
You meant if a class could look like a struct to C, surely.
---if it could, it will be accessible from C, and probably
from most other languages as well
(on typical small and medium
general purpose computers
That covers a lot (the most of it?) of ground, and propably shouldn't be
flattened.
---I wouldn't count on it on
mainframes, nor for that matter on embedded systems); otherwise,
it won't be. (You may, of course, add non-virtual member
functions, including conversion operators, but not constructors,
destructors or assignment operators, without causing problems.)
I'm still hoping that Mr. Coffin was right about "convenience constructors"
being OK. (Else I'll have to research and find out why there cannot be such.
I'll blindly accept the concrete "special functions" as being special and
reserved, but I really would want to know why "convenience constructors" are
a no-go, if indeed so).
The other thing that is often important is whether the class is
an aggregate which supports static initialization. But in
practice, the two overlap; there are very few cases where you
can use static aggregate initialization but couldn't write the
class in C. (The presence of a pointer to member in the class
would be an example of one.)
I should have stopped responding to your post probably after I asked whether
you meant the current standard or the yet-to-be (draft) C++. All of a
sudden, I feel like I know NOTHING! Errrgh!! I think my house may explode if
I start up Visual C++ and _I_ use it! I saw no red sticker on the box.. I
got it for free as a download!
A conversion operator is just an ordinary member function.
A positive note for the post to end on! At least something is concrete!
(Until someone says a POD can't have member functions!)
(Aside: Are overloaded operators fast compared to functions? I don't know
why that sticks in my mind. Yeah I know, I could test it, but I think in the
past I have and have found that. I may be mistaken).