c++ - 关联容器的迭代器不依赖于比较器模板参数

标签 c++ templates visual-studio-2013 stl iterator

编译器无法区分两种不同类型的迭代器。

请在此处查看两种类型。

typedef std::set<int, std::greater<int> >             PriceBookBuy;
typedef std::set<int, std::less<int> >                PriceBookSell;

typedef PriceBookBuy::iterator PriceBookBuyIter;
typedef PriceBookSell::iterator PriceBookSellIter;

我想重载一个基于两种迭代器类型的模板方法。

class LVOrderBookBin
{
    int a;
    int b;
    public:

   template<typename PriceBookBuy>
   void erasePriceLevel(std::vector<PriceBookBuyIter> remover) throw()
   {
            b++;
            remover.begin();
        }
        template<typename PriceBookSell>
        void erasePriceLevel(std::vector<PriceBookSellIter> remover) throw()
        {
            a++;
            remover.begin();
        }
};

典型用法:

int main()
{
    std::vector< PriceBookBuyIter> vecBuy(3);
    std::vector< PriceBookSellIter> vecSell(3);


    LVOrderBookBin lv;
    lv.erasePriceLevel<PriceBookBuy>(vecBuy);
    lv.erasePriceLevel<PriceBookSell>(vecBuy);
}

用于使用 Vs2008 的相同代码,但不能使用 VS2013 进行编译。它也不能用 gcc 编译。

错误:‘template void LVOrderBookBin::erasePriceLevel(std::vector, std::allocator >>)’无法重载

有解决办法吗?

最佳答案

标准库的实现完全在他们的权利范围内,并且几乎成为强制性的。您看到的效果被称为 SCARY 迭代器,这是一件好事。

您必须将迭代器包装在自定义结构中 - 使用您自己的自定义类型有效地标记它们才能以这种方式使用 OR。

关于c++ - 关联容器的迭代器不依赖于比较器模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29795291/

相关文章:

c++ - 成员函数的模板和指针

c++ - 单链表交换函数(int, int)

c++ - python distutils 不包括 SWIG 生成的模块

c++ - 64位减法结果转32位整数

c++ - gdb 将内存地址解释为对象

mysql - 在网页上显示来自数据库的表格

javascript - 合并 js 文件时 Typescript Intellisense 不起作用

android - 如何从 Android 中的 C++ 库调用方法?

c++ - 使用可变参数模板创建哈希队列

c++ - std::bind 分配给 std::function