Re: tree_node using std::vector
On 17 May, 22:31, Daniel Kr?gler <daniel.krueg...@googlemail.com>
wrote:
You can definitely not nominate this as portable, because
the standard says that it will cause undefined behavior
([lib.res.on.functions]/2, last bullet). Seemingly working
code is one possible outcome of undefined behavior, see
[intro.compliance], footnote 3:
If I understand the standard correctly, the following code
is not portable because foo::v is required to be complete
and it forces std::vector<foo> to be instantiated, which
is UB.
#include <vector>
struct foo {
foo();
~foo();
foo(const foo&);
foo& operator=(const foo&);
private:
std::vector<foo> v;
};
foo::foo() {}
foo::~foo() {}
foo::foo(const foo& a) : v(a.v) {}
foo& foo::operator=(const foo& a) {
v = a.v;
return *this;
}
Now I'm wondering why standard puts such a restriction
on std::vector and other containers? FWIW, it's easy
to implement std::vector in such a way that it gives
the previous example defined and correct behavior.
In fact, all implementations of std::vector I'm aware
of allow instantiating it with incomplete type (unless
they have a special check similar to the one in concept
gcc).
It's a very nice property of standard containers to be
instantiatable with a incomplete type. Why ban such
feature? Can it be made at least implementation defined,
in which case it will become a matter of quality of
standard library?
Soon we'll have concepts-powered STL libraries and
it's likely that standard library vendors will add
all the concept checks described by the standard in
their implementation of STL (that's what you can
find in the newest STL from gcc). And after that we
won't be able to instantiate vector with incomplete
type while now we practically can do that. What
should we do? Just copy all the containers from
namespace std to namespace mystd and remove this
redundant and useless check.
I must admit that this problem is very loosely
related to concepts, because the restriction
regarding instantiation of containers with incomplete
types was in the standard for a long time. But
instead of removing it from standard it will be
enforced by concept checks, which is not cool, imho.
Roman Perepelitsa.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]