I encountered a problem which i dont really know how to solve. I want to pass variadic ammount of pairs in constructor, using { } braces, but for some reason im gettin compialtion error. I kept class as simple as possible
include
class MyPairs { public: MyPairs(std::pair&& pair) { pairs[pair.first] = pair.second; }
template <typename ... Args>
MyPairs(std::pair<char, int>&& pair, Args&& ... args)
{
pairs[pair.first] = pair.second;
MyPairs(std::forward<Args>(args)...);
}
private:
std::map<char, int> pairs;
};
int main() {
MyPairs p1( // okay, constructor expects pairs
std::pair<char, int>('a', 1),
std::pair<char, int>('b', 2)
);
MyPairs p2( // compiler knows that it is std::pair<char, int> in constructor
{ 'c',3 }
);
MyPairs p3( // error, for some reason now he doesnt know it is std::pair<char, int>, std::pair<char, int>
{ 'd',6 },
{ 'e',5 }
);
}
Of course i can write std::pair('a', 1) for every argument, but { 'a', 1} is much more conventient.
Why compiler doesnt know how to match function when using { } braces ?
Greetz
Aucun commentaire:
Enregistrer un commentaire