The following gives me compiler error:
could not deduce template argument for 'const std::weak_ptr<_Ty> &' from 'std::shared_ptr'
#include <memory>
class Foo
{
public:
template<typename R>
void Bar(std::weak_ptr<R> const & p)
{
p;
}
};
int main(void)
{
auto foo = Foo();
auto integer = std::make_shared<int>();
foo.Bar(integer);
}
I tried,
template<typename R>
void Bar(std::weak_ptr<R::element_type> const & p)
{
}
, which seems syntatically incorrect. The following works but I wondered if it's possible to get the conversion in p, without creating another temporary?
template<typename R>
void Bar(R const & p)
{
auto w = std::weak_ptr<R::element_type>(p);
}
To be clear I'd like to explicitly state the function should take a shared or weak_ptr, so I don't like the R const & p solution.
For completeness this also works of course:
template<typename R>
void Bar(std::shared_ptr<R> const & p)
{
auto w = std::weak_ptr<R>(p);
}