I have a function requiring const std::vector<std::shared_ptr<const T>>& as signature. It seems that compiler doesn’t allow me to pass in a std::vector<std::shared_ptr<T>>. Wondering why it cannot implicitly convert from std::vector<std::shared_ptr<T>> to std::vector<std::shared_ptr<const T>>.
I tried passing an object of std::vector<std::shared_ptr<T>> to a function requiring const std::vector<std::shared_ptr<const T>>& as signature. It fails to compile.
>Solution :
Since there is no implicit conversion between
std::vectors with different template arguments. Just write helper function:
–
Marek R
class Foo {};
std::vector<std::shared_ptr<const Foo>> convert(const std::vector<std::shared_ptr<Foo>>& v)
{
return {v.begin(), v.end()};
}