template class in member function



  • hallo leute

    wie sieht es mit template-klassen in member funktionen aus ? gibt es da bestimmte beschränkungen ?

    folgende template-klasse:

    template<class CHAR_TYPE>
    class property_runtime_information : public IRuntimeInformation
    {
       public:
          virtual auto __stdcall ClassName(void) const noexcept -> const char*
          {
             template<class CHAR_TYPE>;
    
             template<> 
             struct property_class_name<char> 
             { 
                static constexpr const char *string = "xpe::basic_property<char>"; 
             };
    
             template<> 
             struct property_class_name<unsigned char> 
             { 
                static constexpr const char *string = "xpe::basic_property<unsigned char>"; 
             };
    
             template<> 
             struct property_class_name<wchar_t> 
             { 
                static constexpr const char *string = "xpe::basic_property<wchar_t>"; 
             };
    
             template<> 
             struct property_class_name<unsigned int> 
             { 
                static constexpr const char *string = "xpe::basic_property<unsigned int>"; 
             };
    
             return property_class_name<CHAR_TYPE>::string;
          }
    
          virtual auto __stdcall Size(void) const noexcept -> size_type
          {
             return sizeof(basic_property<CHAR_TYPE>);
          }
    
          virtual auto __stdcall IsCloneable(void) const noexcept -> bool
          {
             return true;
          }
    }; /* class property_runtime_information */
    
    template<class CHAR_TYPE>
    inline auto basic_property<CHAR_TYPE>::RuntimeInformation(void) const noexcept -> const IRuntimeInformation *
    {
       static const property_runtime_information<CHAR_TYPE> ri;
    
       return &ri;
    }
    

    als eigenstaendige klasse in einem namespace gibt es kein problem.
    in einer member-funktion einer template-klasse gibt es allerdings einen fehler:

    template<class CHAR_TYPE>
    inline auto basic_property<CHAR_TYPE>::RuntimeInformation(void) const noexcept -> const IRuntimeInformation *
    {
       template<class CHAR_TYPE>
       class property_runtime_information : public IRuntimeInformation
       {
          public:
             virtual auto __stdcall ClassName(void) const noexcept -> const char*
             {
                template<class CHAR_TYPE>;
    
                template<> 
                struct property_class_name<char> 
                { 
                   static constexpr const char *string = "xpe::basic_property<char>"; 
                };
    
                template<> 
                struct property_class_name<unsigned char> 
                { 
                   static constexpr const char *string = "xpe::basic_property<unsigned char>"; 
                };
    
                template<> 
                struct property_class_name<wchar_t> 
                { 
                   static constexpr const char *string = "xpe::basic_property<wchar_t>"; 
                };
    
                template<> 
                struct property_class_name<unsigned int> 
                { 
                   static constexpr const char *string = "xpe::basic_property<unsigned int>"; 
                };
    
                return property_class_name<CHAR_TYPE>::string;
             }
    
             virtual auto __stdcall Size(void) const noexcept -> size_type
             {
                return sizeof(basic_property<CHAR_TYPE>);
             }
    
             virtual auto __stdcall IsCloneable(void) const noexcept -> bool
             {
                return true;
             }
       }; /* class property_runtime_information */
    
       static const property_runtime_information<CHAR_TYPE> ri;
    
       return &ri;
    }
    

    Fehler:

    1> property.xpe.hpp(323): error C2440: "return": "const xpe::basic_property<unsigned int>::RuntimeInformation::property_runtime_information<CHAR_TYPE> *" kann nicht in "const xpe::IRuntimeInformation *" konvertiert werden
    1>          with
    1>          [
    1>              CHAR_TYPE=unsigned int
    1>          ]
    1> property.xpe.hpp(323): note: Die Typen, auf die verwiesen wird, sind nicht verknüpft; die Konvertierung erfordert einen reinterpret_cast-Operator oder eine Typumwandlung im C- oder Funktionsformat.
    

    woran liegt das ?

    Meep Meep


  • Mod

    Meep Meep schrieb:

    hallo leute

    wie sieht es mit template-klassen in member funktionen aus ?

    Klar. Templatedeklarationen dürfen nur auf Namensraumebene oder in einer Klassendefinition auftreten.


  • Mod

    template<class CHAR_TYPE>;
    

    Hä?



  • Arcoth schrieb:

    template<class CHAR_TYPE>;
    

    Hä?

    ja da is beim kopieren was schief gegangen.

    heisst natuerlich

    template<class CHAR_TYPE> struct property_class_name;
    

Anmelden zum Antworten