编译器无法区分两种不同类型的迭代器。
请在此处查看两种类型。
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/