簡體   English   中英

關聯容器的迭代器不依賴於Comparator模板參數

[英]Iterator of associative container is not dependent on Comparator template argument

編譯器無法區分兩種不同類型的迭代器。

請參閱此處的兩種類型。

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編譯。

錯誤:'模板無效LVOrderBookBin :: erasePriceLevel(std :: vector,std :: allocator>>)'無法重載

有解決方法嗎?

標准庫實現完全在其權限范圍內,幾乎已成為強制性。 您看到的效果稱為SCARY迭代器,這是一件好事。

您將必須將迭代器包裝在自定義結構中,從而用您自己的自定義類型有效地標記它們,以這種方式使用OR。

這不是專門化模板的正確方法。 您只是在聲明函數重載。 您需要像這樣專門化:

class LVOrderBookBin
{
public: 
    template<typename T>
    void erasePriceLevel(std::vector<typename T::iterator> remover) throw();
};

template<>
void LVOrderBookBin::erasePriceLevel<PriceBookBuy>
     (std::vector<PriceBookBuyIter> remover) throw()
{
    //...
}
template<>
void LVOrderBookBin::erasePriceLevel<PriceBookSell>
     (std::vector<PriceBookSellIter> remover) throw()
{
    //...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM