Boost logo

Boost :

From: Rainer Deyke (rainerd_at_[hidden])
Date: 2003-10-09 14:16:22


E. Gladyshev wrote:
> --- Rainer Deyke <rainerd_at_[hidden]> wrote:
>
>> I can think of at least one degenerate case where your trick fails.
>>
>> class A;
>>
>> A* the_A = 0;
>>
>> class A {
>> A() { the_A = this; }
>> ~A() { the_A = 0; }
>> };
>>
>> class B {
>> B() { if (the_A) do_something_with(the_A); }
>> };
>>
>> variant<A, B> v(A());
>> v = B();
>>
>
> It'll fail as soon as you use placement 'new'
> regadless of this trick.

Does "it" above refer to your code (variant::operator=) or my code (class A
and B above)? Either way, I don't see it. My code does fail when there is
more than one instance of 'A', but that's just because it's cheap untested
throw-away code.

struct A;

std::set<A *> instances_of_A;

struct A {
  A() { instances_of_A.insert(this); }
  A(A const&) { instances_of_A.insert(this); }
  ~A() { instances_of_A.erase(instances_of_A.find(this)); }
};

struct B : A {
};

variant<A, B> v(A());
v = B(); // breaks program invariants

-- 
Rainer Deyke - rainerd_at_[hidden] - http://eldwood.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk