last day (15 days later) » 

5:38 AM
0
A: Check for inheritance at runtime having only one of the types and void*

KaaCame up with one possible solution, works quite well - but it is restrictive in the following ways: I have to specialize TypeTagSpec with inheritance pattern that reflects the original type's inheritance Validation happens on the type layer, rather than object pointer layer (this is clarified i...

 
Your solution has undefined behaviour because reinterpret-casting Derived* to Base* is a no-no.
Here's an illustration and before saying that you don't have multiple inheritance and therefore it's OK try to read the standard and find one behaviour that is well defined in a single inheritance situation and undefined for multiple inheritance. Undefined is undefined. If it happens to produce expected results in your particular case with your particular implementation, it doesn't make it less undefined.
 
Kaa
@n.m.First of all, the non standard-layout you suggested in your “illustration” is called diamond inheritance, and you will not find it in my code. Furthermore, in your “illustration”, you couldn’t even implicitly cast Mixed* to Base* without a static or dynamic cast to either Derived1* or Derived2* - that is in order to resolve the ambiguity.
@n.m. Second of all, reinterpret_cast’ing Derived* to Base* is in fact defined behavior (to be clear - Base* to Derived* is undefined). If you’re making such bold claims, you should cite the standard yourself. Here’s my supporting document: - section: 6 (pay attention to type aliasing) - clarified under Notes as “AliasedType is a (possibly cv-qualified) base class of DynamicType.". Furthermore, it gives an example of what is OK (Derived* to Base*), literally search for "auto p1 = reinterpret_cast<S*>(&s1);”
@n.m The link goes as far as even showing a diamond inheritance example as not ok, obviously. “Illustrating” that for me was very unnecessary. In conclusion, since I intend to upcast only, and I will not have diamond inheritance problems, this answer appears OK so far (though I’m yet to refine it).
 
Let's try again. Reinterpret-casting Derived* to Base* is UB. Reinterpret-casting Base* to Derived* is also UB. The example only illustrates that, it doesn't prove anything. The proof is in the standard. Your quote has nothing to do with reinterpret cast and doesn't support your claim. Single inheritance may appear to work as expected but it is no less UB than the multiple inheritance case (not necessarily diamond BTW, it will also break without the diamond). If you are willing to work with UB, more power to you.
 
Kaa
Like I mentioned earlier, if you're going to make such bold claims - why don't you cite the standard? You're claiming to know it by making such statements, please prove your point. Otherwise, I present documentation - you present a known example of terrible design involving diamond inheritance. The docs I link prove my statement correct, are you able to prove me wrong, and to backup your claim?
Furthermore, my quote makes **perfect sense**, because it describes **exactly** the situation:

struct S { int x; };
struct T { int x; int f(); };
struct S1 : S {}; // standard-layout <- my layout
struct ST : S, T {}; // not standard-layout <- your proposed layout

S s = {};
auto p = reinterpret_cast<T*>(&s); // value of p is "pointer to s"
auto i = p->x; // class member access expression is undefined behavior; s is not a T object
p->x = 1; // undefined behavior
p->f(); // undefined behavior
What is happening here:

auto p1 = reinterpret_cast<S*>(&s1);

Could we be possibly reinterpret_casting a derived type s1 to a based type S?
Just to be clear, this is taken directly from https://en.cppreference.com/w/cpp/language/reinterpret_cast

Please, allow me to also quote section 6 on that page:

"The paragraph defining the strict aliasing rule in the standard contains two additional bullets partially inherited from C:

- AliasedType is an aggregate type or a union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions).
 
5:58 AM
Let's see a little example first.
This has no diamond, no virtual, no nothing. Just two base classes.
Can you reconcile it with your quote about aliasing?
 
Kaa
I can first reconcile it with your very first comment:

"Your solution has undefined behaviour because reinterpret-casting Derived* to Base* is a no-no."

Then I see you say 'undefined' at least another 4 times, and then I see you repeat it:

"Reinterpret-casting Derived* to Base* is UB"

I hate to answer a question with a question, but can we please get past this part first?
Are you able to reconcile your example against the documentation?
 
So which of my claims about UB is false?
 
Kaa
Both claims are identical, and are both false
Now, if we discuss reinterpret_casting <void*> to <Base*> - we would be having a different conversation - I'm still thinking about it
 
So why does the little example I just shown isn't working?
So why isn't the little example I just shown working?
 
Kaa
It's interesting, I need a moment to review closer n.m
Oh simple
 
6:05 AM
At what point does the standard allow 42 to become 2.07508e-322?
 
Kaa
it's because you're DOWNCASTING :
C * pc = reinterpret_cast<C*>(pb2);
 
I'm both downcasting and upcasting in the example as you can see
 
Kaa
Oh wait
 
C * pc = reinterpret_cast<C*>(pb2);
 
Kaa
I misread the first one, my apologies
 
6:06 AM
And neither one works like you would expect
It is abundantly clear why this happens
 
Kaa
The first one surprises me, but I'm not 100% sure on multiple inheritance situation, I need to review closer for a minute
The second one does not surprise me at all, as it openly defies the docs
 
A and B subibjects of C cannot both be at the same address. But reinterpret cast doesn't change the address, only the type.
So when you reinterpret cast C to B and to A, both cannot possibly be right.
One of the subobjects may be at the same address as C, but not both.
Now the thing is, the standard doesn't guarantee you that either one is at the same address.
It just so happens with most implementations (but not all!)
Even if there is only one base class, its subobject is not guaranteed to be at the same address as the most derived object.
And this is the reason either cast is undefined.
I mean, the cast itself is OK but accessing the object through the resulting pointer is undefined.
Your quote from cppreference is rather misleading. It is only applicable when there is in fact an object of AliasedType at that address. But this is exactly what we don't have here.
You need to make this correction, otherwise when you look at the first bullet point, you may conclude that you can reinterpret cast struct x { int a; double b; } address to double*, which is obviously false.
You can however reinterpret cast it to int because there happens to be an int at the same address as the struct.
Sorry have to go now, may contunue later today
 
Kaa
Hmm indeed, casting won't work - but I'm struggling with subobject concept, I don't know how C++ handles it (outside of what you're saying and demonstrating)
Thanks, I have things to read - appreciate the time
 
Kaa
6:33 AM
I understand you now
"Even if there is only one base class, its subobject is not guaranteed to be at the same address as the most derived object." - as perplexing as it is to read, I'm starting to see your point. I'm going to see if I can verify this claim against the standard
Regarding multiple inheritance - your point is very logical and makes sense, thank you for sharing this. It seems that no casting will save me from the void* to object upcast situation in multiple inheritance, but I'd still like to make sure about single inheritance.
You are completely right
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf

Chapter 10, section 5:
"The order in which the base class subobjects are allocated in the most derived object (1.8) is unspecified"
So this completely supports your claim. It means I'm just lucky that that the subobject Base was at the same address as Derived, at least in the compiler I'm using
And of course this will never be true in multiple inheritance. Everything checks out, thank you n.m. - I'll make a note in my answer
 

last day (15 days later) »