Consider the following example:
#include <memory>
template<class T>
class RefSP {
private:
std::shared_ptr<T> p;
public:
template<class A>
RefSP(A&& v) : p(std::forward<A>(p)) {}
};
template <
class T,
class U,
typename std::enable_if<std::is_base_of<std::shared_ptr<U>, T>::value>::type* = nullptr>
inline RefSP<U>*
make_ref_sp(T&& p)
{
return new RefSP<U>(std::forward<T>(p));
}
int main()
{
auto sp = std::make_shared<int>(42);
auto x = make_ref_sp(sp);
}
I get compilation error
In function 'int main()':
25:28: error: no matching function for call to 'make_ref_sp(std::shared_ptr<int>&)'
25:28: note: candidate is:
17:1: note: template<class T, class U, typename std::enable_if<std::is_base_of<std::shared_ptr<_Tp2>, T>::value>::type* <anonymous> > RefSP<U> make_ref_sp(T&&)
17:1: note: template argument deduction/substitution failed:
25:28: note: couldn't deduce template parameter 'U'
The question is, how can I fix the code so that U will be inferred given that T is std::shared_ptr<U> while preserving move-semantics.
Aucun commentaire:
Enregistrer un commentaire