last day (15 days later) » 

6:15 PM
0
Q: Call to derived class function in vector populated with variadic initializer list doesn’t behave polymorphically

rtischer8277I am using an initializer list to declare an arbitrary number of item pairs at compile time, each consisting of a key and a real type as the value. There are two derived real types in the model code: MyA and MyB, both of which derive from My. The vector gets initialized with a key and a particula...

 
Slicing. You can't store derived objects by value as base objects. Use std::unique_ptr's to add the required indirection.
 
But where would I create the std::unique_ptr? Easy to declare, but not sure how I would do a std::make_unique.
 
typedef ItemPair< const int, std::unique_ptr<My>> IPIntMy;, and then make_unique when you fill your container in main() :)
 
But I would like for the container to be filled without having to "fill my container" as a separate step. I can see by stepping through the initialization code, that at some point a 'new' is called for pointers to my initialization list values. I can also see that a default 'alloc' method gets called with a 'do nothing' in it. Normally, I would think this means that I can replace the default alloc with the std::make_unique. No?
I just noticed the same question in another posting: "Calling virtual method of base template from derived variadic template class". His answer was to use 'using', but I'm not even sure that is C++ and downcasting. I think std::unique is more logical so I want to get that working.
 
It's not an issue with templates or anything. By storing ValueT val; (i.e My val; after specialization) by value, you can only store a My, not anything derived from it (where would additional fields go ?). This is called slicing, because only the base class part of the derived instance is copied across, while the rest is sliced off. To fix it, you must store a polymorphic handle (pointer or reference) to your base class. std::unique_ptr fits the bill because it's a pointer which ensures automatic lifetime by default. [cont.]
[cont.] I'm not sure what you mean with "separate step", just replace IPIntMy { 33, MyA {} } with IPIntMy { 33, std::make_unique<MyA>() }.
 
6:17 PM
I get this error: error C2280: 'std::unique_ptr<My,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function. Is there a way I can move the newly constructed std::make_unique<MyA>() obj?
 
'evening !
the result of std::make_unique is an xvalue, so moving it directly into the constructor shouldn't be a problem
otherwise, you can move the pointee explicitly with std::move(ptr)
you'll want to initialize val this way (val(std::move(val)))
 
afternoon:-)
ItemPairVect itemVect1 { IPIntMy { 33, std::move( std::make_unique< MyA >() ) }, IPIntMy { 44, std::move( std::make_unique< MyB >() ) },
IPIntMy { 55, std::move( std::make_unique< MyA >() ) }, IPIntMy { 66, std::move( std::make_unique< MyA >() ) } };
error C2280: 'std::unique_ptr<My,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
is this what you meant?
 
no, your initializer_list is fine, it's the initialization of val that is causing an issue
inside the constructor of ItemPair, the val parameter is an lvalue
it can be initialized by moving from the temporary that make_unique returns, so the calling site is ok
however, you must then explicitly move that parameter inside the val member variable
 
6:34 PM
got it! Like, : key( key ), val( std::move( val ) ) {}
but: error C2280: 'ItemPair<const int,std::unique_ptr<My,std::default_delete<_Ty>>>::ItemPair(const ItemPair<const int,std::unique_ptr<_Ty,std::default_delete<_Ty>>> &)': attempting to reference a deleted function
 
Mhh, that's embarassing. The problem is with the initializer_list.
an initializer_list gives you const access only to its elements
this problem has been asked about here : stackoverflow.com/questions/8193102/…
and sadly there is no way to move from initializer_lists for now.
there's a recent paper about a fix here : open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4166.pdf
but until it's incorporated to the standard, move semantics and initializer lists will be painful combination.
 
so, use std::shared_ptr instead I guess
for now
 
yep, that's the cleanest way I see.
you could also use a variadic constructor, but that's a bit more work.
 
6:51 PM
works fine. Much obliged for your assistance.
 
my pleasure ! have a nice day :)
 

last day (15 days later) »