C ++模板专门化提供额外的成员函数? [英] C++ Template specialization to provide extra member function?

查看:280
本文介绍了C ++模板专门化提供额外的成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以非内联方式为专用模板提供额外的成员函数?
ie。

how do I provide extra member function for specialized template in a non-inline way? i.e.

template<typename T>
class sets
{
    void insert(const int& key, const T& val);
};
template<>
class sets<bool>
{
    void insert(const int& key, const bool& val);
    void insert(const int& key){ insert(key, true); };
};

但是当我写 sets< bool&键)

template<>
class sets<bool>
{
    void insert(const int& key, const bool& val);
    void insert(const int& key);
};
template<>
void sets<bool>::insert(const int& key)
{
    insert(key, true);
}

GCC抱怨:

template-id'insert<>'for'void
ip_set :: insert(const int&)'
不匹配任何模板声明

template-id ‘insert<>’ for ‘void ip_set::insert(const int&)’ does not match any template declaration


推荐答案

这是因为它不是你的模板的函数,所以不要使用template<>。它适用于我删除模板<>如下:

That's because it is not a function of your template so don't use "template<>". It works for me after removing "template<>" as below:

void sets<bool>::insert(const int& key) 
{ 
    insert(key, true); 
}

我的系统FC9 x86_64。

My system FC9 x86_64.

整个代码:

template<typename T>
class sets
{
public:
    void insert(const int& key, const T& val);
};

template<>
class sets<bool>
{
public:
    void insert(const int& key, const bool& val) {}
    void insert(const int& key);
};

void sets<bool>::insert(const int& key)
{
    insert(key, true);
}

int main(int argc, char **argv)
{
        sets<bool> ip_sets;
        int key = 10;
        ip_sets.insert(key);
        return 0;
}

这篇关于C ++模板专门化提供额外的成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆