I have the following example code. It compiles on clang but it doesn't on Visual Studio 2013.
#include <iostream>
#include <utility>
#include <string>
using namespace std;
void f(const pair<string, string>& p)
{
cout << p.first << ", " << p.second << endl;
}
void f(initializer_list<pair<string, string> > ps) {
for (auto p : ps) f(p);
}
int main()
{
f({ "2", "3" });
f({ { "2", "3" }, { "3", "4" } });
}
The second call to f fails to compile with:
1error C2668: 'f' : ambiguous call to overloaded function
could be 'void f(std::initializer_list<std::pair<std::string,std::string>>)'
or 'void f(const std::pair<std::string,std::string> &)'
1> while trying to match the argument list '(initializer-list)'
If I use pairs of int instead of pairs of string it does work fine.
Is anybody aware of a problem like this in Visual Studio? Or am I doing something wrong?
Thank you.
f(pair(string("2", "3"), string("3, 4"))), using the constructor ofstd::stringthat takes a pair of iterators (const char*pointers are valid iterators), in addition to "initializer list of pairs" interpretation. Off the top of my head, I can't say whether such an interpretation is legal.pair?std::pairis an aggregate; this is why brace-initializer works for it.std::pairis not an aggregate - it's got plenty of user-provided constructors.