Consider this:
#include <iostream>
#include <map>
#include <string>
#include <boost/assign/list_of.hpp>
using boost::assign::map_list_of;
const std::map<int, std::map<int, char> > test = map_list_of
(100, map_list_of
(1, 'a')
(2, 'b')
)
(101, map_list_of
(1, 'c')
(2, 'd')
)
;
int main()
{
std::cout << test.find(101)->second.find(2)->second << "\n";
}
I wanted the result to be a program that, when executed, outputs d
.
Instead, I get this:
$ clang++ -std=c++03 -O2 -Wall -pedantic -pthread main.cpp
In file included from main.cpp:1:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/iostream:39:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/ostream:38:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/ios:40:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/char_traits.h:39:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:64:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_pair.h:119:22: error: call to constructor of 'std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > >' is ambiguous
: first(__p.first), second(__p.second) { }
^ ~~~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_tree.h:1843:29: note: in instantiation of function template specialization 'std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > >::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > >' requested here
_M_insert_unique_(end(), *__first);
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_map.h:255:16: note: in instantiation of function template specialization 'std::_Rb_tree<int, std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > >, std::_Select1st<std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > > >, std::less<int>, std::allocator<std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > > > >::_M_insert_unique<std::_Deque_iterator<std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > >, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > &, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > *> >' requested here
{ _M_t._M_insert_unique(__first, __last); }
^
/usr/local/include/boost/assign/list_of.hpp:163:20: note: in instantiation of function template specialization 'std::map<int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > >, std::less<int>, std::allocator<std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > > > >::map<std::_Deque_iterator<std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > >, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > &, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > *> >' requested here
return Container( begin(), end() );
^
/usr/local/include/boost/assign/list_of.hpp:142:20: note: in instantiation of function template specialization 'boost::assign_detail::converter<boost::assign_detail::generic_list<std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > >, std::_Deque_iterator<std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > >, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > &, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > *> >::convert<std::map<int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > >, std::less<int>, std::allocator<std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > > > > >' requested here
return convert<Container>( c, tag_type() );
^
/usr/local/include/boost/assign/list_of.hpp:436:49: note: in instantiation of function template specialization 'boost::assign_detail::converter<boost::assign_detail::generic_list<std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > >, std::_Deque_iterator<std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > >, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > &, std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > *> >::convert_to_container<std::map<int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > >, std::less<int>, std::allocator<std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > > > > >' requested here
return this-> BOOST_NESTED_TEMPLATE convert_to_container<Container>();
^
main.cpp:7:50: note: in instantiation of function template specialization 'boost::assign_detail::generic_list<std::pair<int, boost::assign_detail::generic_list<std::pair<int, char> > > >::operator map<std::map<int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > >, std::less<int>, std::allocator<std::pair<const int, std::map<int, char, std::less<int>, std::allocator<std::pair<const int, char> > > > > > >' requested here
const std::map<int, std::map<int, char> > test = map_list_of
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_map.h:171:7: note: candidate constructor
map(const _Compare& __comp,
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_map.h:182:7: note: candidate constructor
map(const map& __x)
^
1 error generated.
(similar results under GCC)
How can I resolve this?
I get a similar error even if I use std::map<int, char>(map_list_of(...))
instead of map_list_of(...)
for those inner maps.
C++03 defines two constructors for
map
that can be called with one argument [lib.map]p2:boost's
map_list_of
creates an object of ageneric_list
class template instantiation, from the recent SVN:Where the primary
generic_list
template contains the following conversion operator:Both
map
constructors are viable, as this operator allows conversion to bothmap
andCompare
. As far as I know, you cannot SFINAE-constrain a conversion operator in C++03.The
map
is constructed explicitly when inserting a new node in the outer map. A pair of iterators is used to iterate over the innergeneric_list
to construct the outermap
. Dereferencing this iterator yields astd::pair<int, boost::assign_detail::generic_list<std::pair<int, char> >
. The node (value) type of the outer map isstd::pair<int const, std::map<int, char> >
.Therefore, the compiler tries to construct the latter type from the former. In C++03, this
pair
constructor is not SFINAE-constrained, since that's not possible in C++03. [lib.pairs]p1libstdc++ implements this as follows:
I'm not entirely sure if that's compliant, since [lib.pairs]p4
(But, as I said, SFINAE on ctors cannot be implemented in C++03.)
In C++11 and 14, this also fails, but for a different reason. Here, the pair constructors are SFINAE-constrained. But the constrain requires implicit convertibility (
is_convertible
), while the program has UB if the target pair of types cannot be constructed from the sources (is_constructible
). I've written a bit more about this issue in another SO answer. Interestingly, a proposed solution N4387 to the issue mentioned in that other question says:This is exactly the case we run into here: A
map
can be copy-initialized from ageneric_list
, since this makes theexplicit
constructor non-viable. But amap
cannot be direct-initialized fromgeneric_list
, since this makes the conversion ambiguous.As far as I can see, N4387 does not solve the problem in the OP. On the other hand, with uniform initialization, we have an alternative to
map_list_of
. And we can SFINAE-constrain conversion operators since C++11.One solution is to eliminate the
explicit
constructor by only allowing implicit conversions:But there's a more direct way: simply use the
convert_to_container
member function ofgeneric_list
's base classconverter
(also a class template):