mardi 3 novembre 2015

Templated class fails to implicit-cast from std::shared_ptr

Failing code is as follows:

#include <memory>

template<size_t N>
class A
{};

template<size_t N>
int testFct(std::shared_ptr<const A<N>> aptr) { return 1; }

int main() {
    auto APtr = std::make_shared<A<2>>();
    testFct(APtr);
}

It is possible to cast implicitly from std::shared_ptr<T> to std::shared_ptr<const T> in similar situations - why do I get an error informing me that 'std::shared_ptr<A<2ul> >' is not derived from 'std::shared_ptr<const A<N> >'?

The problem is presumably that there is no relation between A<N> and A<2>, but the function is templated to only take one N. Can I arrange the function to avoid (re?)templating the class?

On the other hand, adding a function

template<size_t N>
int testFct(std::shared_ptr<A<N>> aptr) { 
    return testFct(static_cast<std::shared_ptr<const A<N>>>(aptr)); 
}

allows the code to compile (and presumably run without issue).

Aucun commentaire:

Enregistrer un commentaire