考慮下面的代碼,使用g ++編譯7.0.1(-std = C++ 17):的std ::元組映射到元組,並使用佈設
#include <map>
#include <tuple>
int main()
{
// Create an alias for a tuple of three ints
using ThreeTuple=std::tuple<int,int,int>;
// Create an alias for a map of tuple to tuple (of three ints)
using MapThreeTupleToThreeTuple=std::map<ThreeTuple,ThreeTuple>;
MapThreeTupleToThreeTuple m;
// The following does NOT compile
m.emplace({1,2,3},{4,5,6});
// ..., and neither does this
m.emplace(std::piecewise_construct,{1,2,3},{4,5,6});
}
我曾想到的是,initializer_list
參數到map::emplace()
就足夠了,並且會導致按照指定將元組鍵值插入到元組值關聯中。顯然,編譯器不同意。
當然,明確地創建一個元組(即ThreeTuple{1,2,3}
,而不是僅僅{1,2,3}
),並傳遞一個以map::emplace()
解決了這個問題,但爲什麼不能在初始化列表被直接傳遞給map::emplace()
這會自動將它們轉發到元組構造?
初始值設定項列表沒有類型。 'emplace'依靠類型扣除。你無法推斷出無類型的東西。 – NathanOliver
關於這個問題,你認爲C++ 17中有什麼改變? – Walter
@Walter,我並不是想暗示我正在使用新的C++ 17功能,只是說我正在使用C++ 17兼容級別。 –