I read the recent thread about casting Insets up to 
their "real" type and thought I'd try and implement André's 
asAInset, asBInset, asCInset thing using templates.

Unfortunately, it doesn't compile :-(

Is their anyway to do this?

Angus


class Base {
public:
        virtual ~Base() {}

        template <typename T>
        virtual
        T * up_cast() { return 0; }
};

class Derived1 : public Base {
public:
        template<>
        Derived1 * up_cast() { return this; }
};

class Derived2 : public Base {
public:
        template<>
        Derived2 * up_cast() { return this; }
};

#include <iostream>

int main ()
{
        Base * a = new Derived1;
        Base * b = new Derived2;

        std::cout << a << ' ' << a->up_cast<Derived2>() << ' '
                  << a->up_cast<Derived1>() << std::endl;
        return 0;
}


aleem@pneumon:aleem-> cxx -std strict_ansi -o trial trial.C
cxx: Error: trial.C, line 6: "virtual" is not allowed in a function template
          declaration
        virtual
--------^
cxx: Error: trial.C, line 12: explicit specialization is not allowed in the
          current scope
        template<>
--------^
cxx: Error: trial.C, line 18: explicit specialization is not allowed in the
          current scope
        template<>
--------^
cxx: Info: 3 errors detected in the compilation of "trial.C".

Reply via email to