开发者

Call non-specialised template class function from specialized template class function

开发者 https://www.devze.com 2023-02-03 02:07 出处:网络
Is it possible to call a function defined in a non-specialised template class from a specialised template class? Here is an example of what i am attempting:

Is it possible to call a function defined in a non-specialised template class from a specialised template class? Here is an example of what i am attempting:

template <typename T>
struct Convert
开发者_开发百科{
 static inline void toString(unsigned num, unsigned places, std::string& str) { ... }
};

template <>
struct Convert<int8_t>
{
 static inline void toString(unsigned num, std::string& str)
 {
   Convert<int8_t>::toString(num, digitis(num), str);
 }
};

GCC complains that it can't see the non-specialised class function; i.e. I guess it only looks within the specialised class.

Any thoughts?

EDIT

Here is a more concrete example from my code (with a possible solution):

struct NonSpecial { };

template <typename T>
class Convert
{

        template <typename R>
        static inline R fromString(const register char *str, const unsigned str_len)
        {   
            R result = 0;
            //convert str to R
            return result;
        }

        friend class Convert<int8_t>;
        friend class Convert<uint8_t>;
}

template <>
struct Convert<int8_t>     
{
    static inline int8_t fromString(const register char* str, const unsigned str_len = 4)
    {
        Convert<NonSpecial>::fromString<int8_t>(str, str_len);    
    }
};

template <>
struct Convert<uint8_t>     
{
    static inline uint8_t fromString(const register char* str, const unsigned str_len = 3)
    {
        Convert<NonSpecial>::fromString<uint8_t>(str, str_len);    
    }
};

I have other functions - toString(), countDigits(), etc. I have chosen this approach so I can keep the same function names for each type (i.e. don't need toStringU32(), toString32, etc.). I considered template specialization but I don't believe this is possible.


In general, this isn’t possible.

There are different possible solutions but they “cheat”. The first is to hoist off the actual default logic into a different function that is not specialized. Now you can call this function from both toString implementations.

The second alternative entails inheriting from the non-specialized class and passing a special tag as the template argument:

struct BaseClassTag { };

template <>
struct Convert<int8_t> : public Convert<BaseClassTag>
{
 typedef Convert<BaseClassTag> TBase;
 static inline void toString(unsigned num, std::string& str)
 {
   TBase::toString(num, digitis(num), str);
 }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消