I’ve been trying to use auto return type templates and am having trouble. I want to create a function that accepts an STL map and returns a reference to an index in the map. What am I missing from this code to make it compile correctly?
(Note: I’m assuming the map can be initialized with an integer assignment of 0. I will likely add a boost concept check later to ensure it’s used correctly.)
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];
}
An example of code that calls this function would be:
auto Entry = FindOrInitialize(Group, SymbolRate, FecRate, StreamBursts);
Entry++;
Add
typenamebefore MapType in the suffix return type declaration.If you forget to add the
typenameyou would get such kind of error (here GCC 4.6.0) :That would give you something like :
But for what you are trying to do, there’s no need for suffix syntax :
Here if you forget the
typenameyou get an error like :Which is much more explicit!