I have a class with a templated constructor for implicit move conversion, however this constructor should NOT be used for the class (which should only be copy constructible). However, the compiler always tries to use the templated constructor instead of the regular copy constructor.
e.g. With this i get the follow compiler errors, link. (you can just copy paste this code if you want to try it)
struct implementation{};
class my_class
{
my_class(my_class&&); // delete move-constructor... OUCH... COMPILER ERROR
public:
my_class(){}
my_class(const my_class& other) : impl_(other.impl_){}
template<typename T>
my_class(T&& impl) : impl_(std::make_shared<T>(std::move(impl))){} // Still tries to use this...
private:
std::shared_ptr<implementation> impl_;
};
class other_class
{
public:
my_class foo()
{
return instance_; // Wants to use move-constructor???
}
private:
my_class instance_;
};
Any one got an idea how to solve this properly?
Okay, here is my complete overhaul of
my_class:As suggested by others, this works for lvalue and rvalue references by using
std::forwardinstead ofstd::move. Theremove_referenceis necessary because for lvalue references,Tis a reference, andderived&does not derive frombase, butderiveddoes (note the reference).