This question already has an answer here:
Consider the following C++ code:
template<class T>
std::shared_ptr<void> eraseType(std::shared_ptr<T> p) {
return p;
}
template<class T>
std::shared_ptr<T> castType(std::shared_ptr<void> p) {
return p;
}
int main()
{
auto p = eraseType(std::make_shared<int>(42));
auto x = castType<int>(p); <-- does not compile
}
eraseType is used to remove the type T from shared_ptr<T> in order to handle it in a generic way.
Of course, the code fails to compile since caseType is malformed.
Is there a way to create a narrowed shared_ptr<T> from a shared_ptr<void> assuming that we know that the shared_ptr holds a proper T?
Aucun commentaire:
Enregistrer un commentaire