Re: A strange problem about vector~
Greg Herlihy wrote:
On 1/28/07 8:53 PM, in article 524ipiF1mu3vjU1@mid.uni-berlin.de, "Ulrich
Eckhardt" <doomster@knuut.de> wrote:
lfeiman888@gmail.com wrote:
class A
{
public:
typedef vector<A> List;
typedef List::iterator Iter;
};
According to the standard, vector<> can only be instantiated on complete
types. At the point you are using it, A is still an incomplete type.
The "List" typedef doesn't instantiate anything - so it could not be
causing a problem. In fact, both A and vector could be incomplete and
there would still be nothing wrong with the typedef declaration.
Okay, up to that point I'm following you and agree that my wording was
ambiguous.
To demonstrate:
template <class T> class Vector;
class A;
typedef Vector<A> List; // OK
Right, this should work, I think.
However, take a look at the line below the first typedef. There, it uses a
nested type of class vector<A> and that does require that vector<A> is
sufficiently defined. As I understand it, that is the point where it fails.
In any event, the Standard Library's requirement that its components be
instantiated with complete types does not apply to this program. As is
evident from the error message, the program is using a third-party
container library (STLPort) and not the C++ Standard Library - and so is
not governed by its requirements.
STLport is an implementation of the C++ standardlibrary, and as such it aims
to fulfil its requirements. BTW: It is STLport and not STLPort.
but when I compile with STLPort 5.1 under msvc 8.0 it give an error
D:\STLport\stlport\stl/type_traits.h(250) : error C2139: A: an
undefined class is not allowed as an argument to compiler intrinsic
type trait '__has_trivial_copy'
Well, the error message is slightly misleading, but basically it says
that A is not a fully defined class at the point it is used with
something that requires one.
It is the declaration of A that is misleading. After all, the declaration
of A that was posted contains no error like the one being reported.
Therefore there must be more to A's declaration than is known to us - and
somewhere in that part of A that was left out - is where the problem
resides.
The problem is that '__has_trivial_copy' is a trait for use with template
metaprogramming that allows you to determine if you can use memcpy to copy
instances of a type. This is used by that vector implementation to either
use memcpy (for reasons of speed) or to use the copy constructor to copy
its underlying array upon reallocation.
Of course, whether memcpy() is a valid way to copy instances can't be
answered for an incomplete type and A is incomplete at the point it is used
in combination with std::vector<>. The reason I call that error message
misleading is that it talks about an undefined class while it is caused by
an incompletely defined class.
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]