我有以下的代碼:爲什麼不能鏗鏘聲++推斷出lambda的地圖類型?
enum RelationalOperator { LT, LTE, EQ, GTE, GT };
std::map<RelationalOperator, bool (*)(const Point&, const Point&)> ops = {
{ GTE, [](const Point& a, const Point& b) { return a >= b; } },
{ LTE, [](const Point& a, const Point& b) { return a <= b; } },
{ EQ, [](const Point& a, const Point& b) { return a == b; } },
{ GT, [](const Point& a, const Point& b) { return a > b; } },
{ LT, [](const Point& a, const Point& b) { return a < b; } },
};
該代碼是一個模板內而Point
是模板參數。
我試着用auto
更換可變ops
的類型,但是鏘++說:
src/utils.hpp:47:10: error: cannot deduce actual type for variable 'ops' with type 'auto' from initializer list
這是爲什麼?我認爲關鍵字auto
適用於這類情況,類型很長且相當明顯。
這不是*顯而易見你想要一個'map'而不是'multimap'或一個結構數組。 –
請爲導致問題的實際代碼提供[mcve](http://stackoverflow.com/help/mcve)。 –
@BoPersson我不好,我沒有考慮到其他對象可以有相同的初始化列表的事實! – rubik