Gunnar G said:
Hello.
My compiler (GCC 3.3.*) does not complain about the following:
#include <iostream>
#include <vector>
using namespace std;
struct X{
int a,b,c;
vector<X> pp;
};
int main(){
X a;
}
So there is not a problem with this self-refering struct, why?
Must not vector know the size of what it should contain?
This was discussed some time ago both on this newsgroup and
comp.lang.c++.moderated. There are two statements in the standard that are
relevant. The first is in 14.3.1 p2 of the 2003 standard. It says:
"[Note: a template type argument may be an incomplete type (3.9). ]"
The second is in section 17.4.3.6 of the standard. It says:
"In certain cases (replacement functions, handler functions, operations on
types used to instantiate standard library template components), the C + +
Standard Library depends on components supplied by a C + + program. If these
components do not meet their requirements, the Standard places no
requirements on the implementation.
"In particular, the effects are undefined in the following cases:
[snip]
- if an incomplete type (3.9) is used as a template argument when
instantiating a template component."
The second quotation prompts the question of exactly when "instantiation"
takes place. The newsgroup discussion suggested that the use of the
vector<X> pp; in your struct declaration DOES constitute instantiation for
the purposes of the quoted passage.
The general conclusion from the discussion (as interpreted by me) was:
1. In general, there is no language bar against template arguments that are
incomplete types in the contexts that you are discussing,
2. Whether or not incomplete types are allowed in this context depends on
the details of the template class concerned. The passage from 17.4.3.6 says
that the standard library is not required to accept incomplete types as
arguments. This was originally because it was not known if the standard
library could be implemented in such a way as to allow incomplete types (in
fact, there were some claims that vectors could not be implemented in such a
way).
The Dinkumware library (which ships with VC++ 7.1, among others) does allow
containers with incomplete types in the context you have described. gcc
apparently does too. Now that its technical feasibility has been shown, it
is likely to make it into the standard. For the present, however, the
property is implementation-dependent.
Two of the original discussions are here (I seem to remember a third thread
but can't find it):
http://groups.google.com/groups?hl=...dZ_b.1662%24SZ.61136%40news.xtra.co.nz&rnum=4
and here
http://groups.google.com/groups?hl=...02170837.7f182aab%40posting.google.com&rnum=1