Template-Default-Argumente



  • Hallo,

    ich habe folgenden Quelltext:

    //Basisklasse für Ausnahmen
    //Template deswegen, damit man das auch mit Unicode verwenden kann
    template<class _CharT = char,class _Traits = std::char_traits<_CharT> >
    class Exception
    {
      private:
        typedef Exception<_CharT,_Traits> base;
        typedef std::basic_string<_CharT,_Traits> string_type;
        string_type message;
      public:
        Exception() : message() { }
        Exception(const string_type& message) : message(message) { }
        Exception(const base& other) : message(other.message) { }
    
        virtual const string_type operator () () const { return message; };
        virtual operator string_type() const { return message; };
        virtual const string_type toString() const { return message; };
    };
    

    Damit ich jetzt nicht lästigerweise immer wieder bei Vererbungen die ganzen Konstruktoren usw. mittippen muss, hab ich mir das folgende Makro geschrieben:

    #define EXCEPTION(name,inherited) \
      template<class _CharT = char,class _Traits = std::char_traits<_CharT> > \
      class name : public inherited<_CharT,_Traits> \
      { \
        private: \
          typedef inherited<_CharT,_Traits> base; \
          typedef std::basic_string<_CharT,_Traits> string_type; \
        public: \
          name() : base() { } \
          name(const string_type& message) : base(message) { } \
          name(const base& other) : base(other.message) { } \
      };
    

    Wenn ich nun an einer anderen Stelle schreibe:

    throw Exception::Size("Fehler beim Erzeugen einer Matrix: Höhe oder Breite ist 0");
    

    ...dann gibt es vom MinGW die folgenden Fehler:

    "main.cpp": main.cpp instantiated from here at line 37
    "vectorial.hpp": datatypes/vectorial.hpp no match for call to `(GeoMath::Exception::Size<_CharT, _Traits>) (const char[58])'
     at line 260
    

    .

    Schreibe ich stattdessen

    throw Exception::Size(std::string("Fehler beim Erzeugen einer Matrix: Höhe oder Breite ist 0"));
    

    ...dann kommt:

    "vectorial.hpp": datatypes/vectorial.hpp no match for call to `(GeoMath::Exception::Size<_CharT, _Traits>) (std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >)' at line 260
    

    Am Makro liegt es jedenfalls nicht. Wenn ich statt Size Exception einsetze, ist es dasselbe in grün.

    Kann sich jemand von euch einen Reim darauf machen? Ich habe den Verdacht, dass das Default-Argument "_CharT = char" nicht berücksichtigt wird.

    Danke schonmal.

    MfG .crypter

    (Der bcc meldet:

    "vectorial.hpp": E2102 Cannot use template 'Exception::Size<_CharT,_Traits>' without specifying specialization parameters in function Matrix<int>::Matrix(const _STL::vector<StaticVector<int>,_STL::allocator<StaticVector<int> > > &) at line 260
    

    )



  • Du benutzt Exception::Size, als ob es eine "normale" Klasse wäre. Um zu zeigen, dass es ein Template ist, gehört noch ein <> hinter Size.



  • Danke, genau das wars. Ich hab mich jetzt so drumherum gemogelt:

    template<class _CharT,class _Traits = std::char_traits<_CharT> >
        class Basic_Exception
        {
          private:
            typedef Basic_Exception<_CharT,_Traits> base;
            typedef std::basic_string<_CharT,_Traits> string_type;
            string_type message;
          public:
            Basic_Exception() : message() { }
            Basic_Exception(const string_type& message) : message(message) { }
            Basic_Exception(const base& other) : message(other.message) { }
    
            virtual const string_type operator () () const { return message; };
            virtual operator string_type() const { return message; };
            virtual const string_type toString() const { return message; };
        };
    
        typedef Basic_Exception<char> Exception;
    

    So machts die Standardbibliothek schließlich auch.

    MfG .crypter


Anmelden zum Antworten