从专用模板类函数调用非专用模板类函数

发布于 2024-10-10 16:25:42 字数 1542 浏览 4 评论 0原文

是否可以从专用模板类调用非专用模板类中定义的函数?这是我正在尝试的一个例子:

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 抱怨它看不到非专门的类函数;即我猜它只在专业类中看起来。

有什么想法吗?

编辑

这是我的代码中的一个更具体的示例(带有可能的解决方案):

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);    
    }
};

我还有其他函数 - toString()、countDigits() 等。我选择了这种方法,这样我就可以保持不变每种类型的函数名称(即不需要 toStringU32()、toString32 等)。我考虑过模板专业化,但我不相信这是可能的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-10-17 16:25:42

一般来说,这是不可能的。

有不同的可能解决方案,但它们“作弊”。第一个是将实际的默认逻辑提升到一个非专门化的不同函数中。现在您可以从两个 toString 实现中调用此函数。

第二种选择需要从非专用类继承并传递一个特殊标签作为模板参数:

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);
 }
};

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);
 }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文