I am trying to build an object that takes std::map as a parameter, passing the contents of the map to it using boost map_list_of .
This gives a compilation error, however, when I try to do the same with a regular function that accepts std::map , it compiles fine!
#include <map> #include <boost/assign.hpp> struct Blah { Blah(std::map<int, int> data) {} }; void makeBlah(std::map<int, int> data) {} int main() { Blah b(boost::assign::map_list_of(1, 2)(3, 4)); // Doesn't compile. makeBlah(boost::assign::map_list_of(1, 2)(3, 4)); // Compiles fine! }
Compilation Error:
error: call of overloaded 'Blah(boost::assign_detail::generic_list<std::pair<int, int> >&)' is ambiguous note: candidates are: Blah::Blah(std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >) note: Blah::Blah(const Blah&)
What is the ambiguity, and why does this not affect the usual functoin makeBlah, which, as far as I can see, has the same signature as the Blah constructor?
And is there a better way to achieve this if you do not make the makeBlah function that will create the Blah object, since it seems to me that I will need to do?
(As an aside, I do this in unit test, using map_list_of to make creating test input more readable)
c ++ constructor boost stl stdmap
nappyfalcon
source share