Re: Using a STL container of type self in a class declaration
On 7 Nov., 05:44, Daniel Kr?gler <daniel.krueg...@googlemail.com>
wrote:
On 6 Nov., 16:29, ros...@gmail.com wrote:
I have a question that I hope is appropriate for this group -
Is the following code compliant to the standard
#include <list>
class B
{
std::list<B> b_list;
};
The reason I ask is because this compiles with gcc and visual studio,
but not with borland c++. I also find it a difficult question to just
look up in the standard, so hopefully somebody can help me out.
The standard says that the behavior of your code is undefined.
We can draw this conclusion by combining [lib.res.on.functions]/2
"In particular, the effects are undefined in the following cases:
[..]
if an incomplete type (3.9) is used as a template argument when
instantiating a template component."
and [class.mem]/2:
"A class is considered a completely-defined object type (3.9) (or
complete type) at the closing } of the class-specifier. Within the
class member-specification, the class is regarded as complete within
function bodies, default arguments and constructor ctor-initializers
(including such things in nested classes). Otherwise it is regarded
as incomplete within its own class member-specification."
I forgot to mention a third necessary paragraph, namely [class.mem]/8:
"Non-static (9.4) members that are class objects shall be objects of
previously defined classes. In particular, a class cl shall not contain
an object of class cl, but it can contain a pointer or reference to an
object of class cl. When an array is used as the type of a nonstatic
member all dimensions shall be specified."
This explains why we can have a function member returning the
currently defined class by value or accepting arguments thereof
by value.
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]