lundi 25 avril 2016

dynamic_cast derived template to less cv-qualified pointer

I've got the following code:

struct Base
{
    virtual ~Base(){};
};

template<class T>
struct Derived : public Base
{};

int main()
{
    Derived<int> d;
    Base *pD = &d;

    if(dynamic_cast<Derived<const int>*>(pD))
    {
        std::cout << "const" << std::endl;
    }

    if(dynamic_cast<Derived<int>*>(pD))
    {
        std::cout << "non-const" << std::endl;
    }
}

I would expect both dynamic_casts to return a valid pointer because the new type is less cv-qualified. Can anyone explain to me what I'm missing? Is there any way to identify a Derived<XYZ> ignoring cv-qualifier given a Base Pointer?

Aucun commentaire:

Enregistrer un commentaire