mercredi 22 juillet 2015

msvc 2013 SFINAE to enable cast operator only from derived to base class

I can't figure out why the commented out lines don't work. I want to limit the implicit cast to casts from derived to base classes. I believe the version of our compiler is vc 2012, even though our version of studio is vs 2013. I tried this code in a few online c++ IDE's and it didn't work in any of them using gcc and c++ 11 either.

#include <type_traits>

template <class T> class CFoo {
public:
    template <class Q>  operator
     // typename std::enable_if<std::is_base_of<Q, T>::value, CFoo<Q>&>::type   // SHOULD WORK?
     // typename std::enable_if<1, CFoo<Q>&>::type                              // SHOULD WORK?
     CFoo<Q>&
     () const {
        return *(CFoo<Q>*)this;
    }
};

class A {};
class B : public A {};

int main(int argc, char* argv[])
{
    CFoo<B> b;
    CFoo<A>& a = b;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire