When the call is made to image move-copy-ctor, calling resource(std::move(o)) don't want bind to resource move-copy-ctor but instead to ctor that excepts string.
How to resolve this?
That will do the wrong thing! I do want it to call resource move-copy-ctor to be able to move data. This code is just short example of what i had in mind. resource ctor that expects string actually allocates some other data.
as if it takes an image, and it cannot implicitly or explicitly conver n to a std::string. To make your code work I just implemented a move-ctor that takes a string object. But that is not what you want, so instead implement the move-ctor for the resource itself..
image&& => resource&& requires a conversion;
the templated constructor requires none and the overload resolves to that.
1 2 3 4 5 6 7 8 9 10 11 12 13
class image : public resource
{
// ...
//image(image&& o) : resource(std::move(o)), img_data(o.img_data)
// a cast from image&& to resource&& is required here to resolve to the base class move constructor
image(image&& o) : resource( static_cast<resource&&>(o) ), img_data(o.img_data)
{
o.img_data = nullptr;
}
// ...
};
> According to Scott Meyers it does not need a cast.
First ask Scott Meyers to add a templated constructor to the base class.
And then ask him once again about not needing a cast.
Why don't you realize that the problem that you are facing has got nothing specific to do with r-value references, move semantics or perfect forwarding. They are just irrelevant, incidental distractions to the real issue - resolving a constructor invocation between a templated constructor and a non-templated constructor.
Compile and run this program (which has nothing that C++98 does not have):