我一直在嘗試使用自動返回類型模板,並遇到了麻煩。我想創建一個接受STL貼圖的函數,並返回對地圖中索引的引用。我從這段代碼中錯過了什麼讓它正確編譯?在模板化函數中使用自動和decltype
(注:我假設地圖可與0的整數轉讓我可能會增加一個Boost概念檢查後,以確保它的正確使用進行初始化)
template <typename MapType>
// The next line causes the error: "expected initializer"
auto FindOrInitialize(GroupNumber_t Group, int SymbolRate, int FecRate, MapType Map) -> MapType::mapped_type&
{
CollectionKey Key(Group, SymbolRate, FecRate);
auto It = Map.find(Key);
if(It == Map.end())
Map[Key] = 0;
return Map[Key];
}
的代碼示例調用該函數是:
auto Entry = FindOrInitialize(Group, SymbolRate, FecRate, StreamBursts);
Entry++;
你收到了什麼編譯錯誤?此外,不需要在這裏使用'decltype'或遲到的返回類型;正常情況下放在左邊。 –
你真的是指「STL」嗎?或者你的意思是「C++ 03標準庫」?或者,給定標籤「C++ 11標準庫」?請選擇一個。 –