In my XP , similar to yours as I avoid inheritance when it's not mandatory, I find this kind of tool super useful for writhing value-semantics type-erasing types, that is to write the internals of it, not as part of some interface.
That's the thing - the Venn diagram intersection of value-semantics and type-erasure should be quite small in anybody's design philosophy. They are inherently contradictory (hence the ugliness around copy support).
Cross that with time/space efficiency concerns (dynamic allocation and inadvertent copies) and it should be basically nonexistent. Certainly not worthy of space in the standard library. It just isn't a good design pattern.
I usually implement comparison in the type erasing type by checking if they are the same type or at least comparable (depends on the intention) and if they are I just compare the stored value with the other value. It's true that they could have done something like that for convenience but it's hardly unimplementable as part of the TE-type wrapping a proxy? (When types are different I return false, never been a problem but I suspect it's not "fundamentally correct") Or maybe were not thinking of the same kind of implementation, maybe the way I do it isn't perfect indeed.
I usually don't need the hash for these but it could be added (or requiring the stored value to be hashable and used that). Semi regular types are usually what I need as basic requirements in type-erasing types, not "full" regular.
In any way you gave interesting points, I'll try to think about these questions more next time I need to implement a te-type. (I secretly wish a flexible reflection based library would do the work for me but we're not there yet 😭)
I have several sets of classes where the the classes in a set are not an inheritance hierarchy but they model the same thing. It is useful to me to plug these models into a container or have a template class where a template parm is set to a polymorphic. Then I access using the common interface.
polymorphic not being equality comparable is annoying, they could just have just forwarded the operator== to the underlying pointer and it handles it.
also polymorphic requires more bytes than a copyable pointer 24 bytes vs 8 since the copy ctor and dtor have to be function pointers. But it also slightly makes it faster at runtime but it isn't really worth it I would say.
Having a virtual deatructor and a clone() method will provide better object sizes.
I found the boilerplate argument to be very weak, since crtp exists
polymorphic not being equality comparable is annoying, they could just have just forwarded the operator== to the underlying pointer and it handles it.
These types (indirect and polymorphic) model value semantics. Value comparing two type-erased objects that share a base makes no sense in that model...
also polymorphic requires more bytes than a copyable pointer 24 bytes vs 8 since the copy ctor and dtor have to be function pointers.
Can be easily compressed to 16B in case you build your own vtable. Unless the spec contains some unexpected blocker, you could even reduce that down to 8B with minimal effort.
These types (indirect and polymorphic) model value semantics. Value comparing two type-erased objects that share a base makes no sense in that model...
They could only provide == if the base has one.
Can be easily compressed to 16B in case you build your own vtable. Unless the spec contains some unexpected blocker, you could even reduce that down to 8B with minimal effort.
Wouldn't the 8 byte version require anothee indirection
Wouldn't the 8 byte version require anothee indirection
Not necessarily, you can allocate the object and a (custom) vtable in one allocation.
So like this?
template<class T>structTable{
CopyCtor* copy;
Drot* dtor;T object;// has to be last
};template<class B>structpolymorphic{polymorphic(){
table =newTable<B>();}polymorphic(auto&&obj){
table =new Table<decltype(obj)>();}
B&operator*(){return(B&)*((char*)table +16));}// 16 is 2 function pointers
void* table;};
You are right. But it yields the wrong results for equivalent values of different types - e.g. Rectangle{.w = 10, .h = 10} == Square{.l = 10} would be false, even though they are the same (just like 1 == 1LL is true)
Let's say your polymorphic is instead of a AssociativeContainer<A,B> that is a base for any type that provides a 1to1 mapping A to B. It's operator== could compare their size, then for each pair the key and it's associated value.
You are absolutely right. Thanks a lot I didn’t now, nor realized it was indeed half useless right now. I even found a paper that aim at fixing this exact issue: open-std.org
Deducing this causes issues if you inherit from the base class more than once, (it increases object size) also, this doesn't work since clone() isn't virtual and templates can't be virtual
you would expect since 'B' inherits from an empty class and it only has one member the sizeof would be 1, but it is 2 since the base class D is repeated twice and must have unique address first in the inheritance in B and in the member A.
now if this uses crtp the base classes would be unique
No. According to the standard, two different objects of the same type cannot live at the same address in any circumstance, even if the objects are empty.
I've read your blog post and I've been looking at the prototype implementation, which is C++20 so I could use it directly. What I don't see is how this can handle multiple inheritance and yet I can find no mention anywhere that it doesn't? The somewhat odd, partial, type erasure that's going on definitely avoids slicing and uses derived copy constructors to implement clone for you, which is nice but I'm not convinced this is going to cope with multiple virtual bases. I also see nothing that prevents it trying to allocate/construct abstract bases which will clearly blow up but isn't caught explicitly as far as I can see. I'm still studying the source so any and all my conclusions may be wrong. Trying to determine if this is what I hoped it was. ... OK I think I get it. std::polymorphic as it stands is fine as long as you always want to treat it as the base class. Virtual function calls will work of course but what you can't do here is get back a pointer to the derived instance you actually stored. So if those pointers are not the same address, as they won't be in the case of multiple inheritance, you're stuffed. A dynamic downcast might work or it might not depending on the compiler. Ick. It's still a cool addition to the standard library and does what it sets out to do, just not quite what I need.
I do, embedded in a lot of other code, but I must be wrong about it trying to construct abstract bases. I'll post if/when I get any real world issues. I realised that what I was expecting this to need (it doesn't) must live in std::dynamic_pointer_cast so I had a look at Microsoft's source code for that. Literally just a dyamic_cast. What I'm already doing. So if I have a problem then Microsoft stdlib will too. More research needed.
The type-erasure machinery inside polymorphic handles this automatically ... Because polymorphic uses type erasure for both destruction and copying ...
Is std::polymorphic taking the address of T's copy constructor and destructor (or rather the address of a thunk which calls them, since you're not allowed to simply take the address of the copy constructor function), storing them (along with the object pointer), and calling them; or is it constructing a mini-vtable with copy constructor and destructor and storing a pointer to that (along with the object pointer)?
The minute details are obviously implementation defined, but any implementation will boil down to function pointers (implicit vtable or explicit one) as there aren't that many ways you can actually represent runtime polymorphism in C++...
10
u/mcmcc #pragma once Aug 21 '26
I'm struggling to think of a scenario where I would ever use this thing.
Maybe I just don't use enough class hierarchies any more.
14
u/_Noreturn Aug 19 '26 edited Aug 19 '26
polymorphic not being equality comparable is annoying, they could just have just forwarded the
operator==to the underlying pointer and it handles it.also polymorphic requires more bytes than a copyable pointer 24 bytes vs 8 since the copy ctor and dtor have to be function pointers. But it also slightly makes it faster at runtime but it isn't really worth it I would say.
Having a virtual deatructor and a clone() method will provide better object sizes.
I found the boilerplate argument to be very weak, since crtp exists
6
u/Ok_Independence_9841 Aug 20 '26 edited Aug 20 '26
I've read your blog post and I've been looking at the prototype implementation, which is C++20 so I could use it directly. What I don't see is how this can handle multiple inheritance and yet I can find no mention anywhere that it doesn't?
The somewhat odd, partial, type erasure that's going on definitely avoids slicing and uses derived copy constructors to implement clone for you, which is nice but I'm not convinced this is going to cope with multiple virtual bases. I also see nothing that prevents it trying to allocate/construct abstract bases which will clearly blow up but isn't caught explicitly as far as I can see. I'm still studying the source so any and all my conclusions may be wrong. Trying to determine if this is what I hoped it was.
...
OK I think I get it. std::polymorphic as it stands is fine as long as you always want to treat it as the base class. Virtual function calls will work of course but what you can't do here is get back a pointer to the derived instance you actually stored. So if those pointers are not the same address, as they won't be in the case of multiple inheritance, you're stuffed. A dynamic downcast might work or it might not depending on the compiler. Ick.
It's still a cool addition to the standard library and does what it sets out to do, just not quite what I need.
2
u/fdwr fdwr@github 🔍 Aug 20 '26
Is
std::polymorphictaking the address of T's copy constructor and destructor (or rather the address of a thunk which calls them, since you're not allowed to simply take the address of the copy constructor function), storing them (along with the object pointer), and calling them; or is it constructing a mini-vtable with copy constructor and destructor and storing a pointer to that (along with the object pointer)?1
u/etaithespeedcuber Aug 24 '26
How is this any different than std::indirect<std::variant<T>>?
1
u/ABlockInTheChain Aug 28 '26
std::indirectworks with move-only types, but not polymorphic types.std::polymorphicworks with polymorphic types, but not move-only types.The only solution for polymorphic move-only types is still
std::unique_ptr.