Re: where does slicing produce UB?
Lance Diduck schrieb:
struct B{
B(unsigned a):data(a){valid();}
B& operator=(B const&r){
data=r.data;
return *this;
}
virtual ~B(){}
virtual void validate(){
valid();
}
protected:
void valid(){
if(data>10)throw 1;
}
unsigned get()const{
return data;
}
private:
unsigned data;
};
struct D:B{
D( unsigned t, unsigned u):B(t),data2(u){
validate();
}
unsigned get2()const{
return data2;
}
D& operator=(D const&r){
B::operator=(r);
data2=r.data2;
return *this;
}
void validate(){
valid();
if((data2+get()>15))throw 2;
}
private:
unsigned data2;
};
void foo(){
auto_ptr<B> b(new D(9,1));
b->validate();//OK
auto_ptr<B> b2(new D(1,10));
b2->validate();//OK
*b2=*b;
b2->validate();//BOOM!!!
D const& d=dynamic_cast<D const&>(*b2);
if(d.get()==9 && d.get2()==10)
cout<<"UDB";
}
I could have just taken out the polymorphic stuff, and just wrote
assertions using get() and get2() and static_cast, and illustrated the
same thing.
The point is that slicing does not change the objects "type" as far as
the symbol table and RTTI and object layout is concerned, however, it
is not longer a D object in terms of the invariants it preserves. Just
what the correct term is for the type of object D is (not a zombie,
that is something else) I don't know.
Sorry, while I understand the "BOOM!" part of your example (yes, this is
shooting into your foot), I do not understand why the "if" clause below,
resp. the calls to d.get() or d.get2(). Why do they trigger UB, or
didn't you want to express this?
Furthermore, I'm not quite clear why the behavior is "undefined". It is
certainly not pleasing, and it certainly breaks the object invariants,
but in how far could a compiler derive from the above "expected
unexpected" result? I would believe that d.get() == 9 and d.get2() == 10
is what the compiler *should* do, so that's AFAIK pretty well-defined.
Just not what the author might have intended.
So long,
Thomas
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]