Frage

I inherit from a template with partial specialization, and I can't call the template ctor from the derived ctor.

When the partial specialization in the code below is commented out, it compiles without any errors or warnings.

#include <iostream>
typedef enum {supertype, subtype} selector;

template< typename T, selector s>
class Tmpl {
protected:
    T* root;
public:
    Tmpl( T* t = 0 ) {
        root = t;
    }
    T listHead( ) {
        std::cout << "template listHead() called" << std::endl;
    }
};

class Descriptor {
public:
    Descriptor( const char * s ) {
        std::cout << "Descriptor " << s << std::endl;
    }
};

// partial specialization - if uncommented, errors 
// are reported at the supertypesIterator ctor below.
/*
template<selector s>
class Tmpl<Descriptor, s> {
public:
    Descriptor listHead( ) {
        switch( s ){
            case supertype:
                return Descriptor("Supertypes");
            case subtype:
                return Descriptor("Subtypes");
        }
    }
};
*/

class supertypesIterator : public Tmpl<Descriptor, supertype> {
public:
    supertypesIterator( Descriptor* t = 0 ):Tmpl<Descriptor, supertype>(t) {}
};


main() {
    supertypesIterator s;
    s.listHead();
}

If I uncomment the specialization, I get the following errors:

$ g++ trouble.cc

trouble.cc: In constructor ‘supertypesIterator::supertypesIterator(Descriptor*)’:
trouble.cc:43:74: error: no matching function for call to ‘Tmpl<Descriptor, (selector)0u>::Tmpl(Descriptor*&)’
trouble.cc:43:74: note: candidates are:
trouble.cc:27:7: note: Tmpl<Descriptor, (selector)0u>::Tmpl()
trouble.cc:27:7: note:   candidate expects 0 arguments, 1 provided
trouble.cc:27:7: note: Tmpl<Descriptor, (selector)0u>::Tmpl(const Tmpl<Descriptor, (selector)0u>&)
trouble.cc:27:7: note:   no known conversion for argument 1 from ‘Descriptor*’ to ‘const Tmpl<Descriptor, (selector)0u>&’

What do I need to do to be able to initialize the base class from within the supertypesIterator ctor?

I'm using g++ version 4.7.1, though I'll also need this to work cross-platform.

War es hilfreich?

Lösung

You have to implement the missing constructor in your specialization. Otherwise, the constructor for supertypesIterator is trying to call a constructor for Tmpl that doesn't exist.

template<selector s>
class Tmpl<Descriptor, s> {
    Descriptor* root;
public:
    Tmpl( Descriptor* t = 0 ) {
        root = t;
    }
    Descriptor listHead( ) {
        switch( s ){
            case supertype:
                return Descriptor("Supertypes");
            case subtype:
                return Descriptor("Subtypes");
        }
    }
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top