
Simply want to grab the keys from a map and assign them to a vector: #include <iostream> #include <boost/bind.hpp> #include <map> #include <vector> #include <string> #include <algorithm> // some dumb object struct Test { int x; int y; Test(int a, int b) : x(a), y(b) {} Test() : x(), y() {}}; int main() { std::pair<std::string, Test> p = std::make_pair("Test", Test(1,2)); // works as I expect std::cout << boost::bind(&std::pair<std::string, Test>::first, _1)(p) << std::endl; std::map<std::string,Test> m; m["Test"] = Test(3,4); std::vector<std::string> keys(m.size()); // won't compile: std::transform(m.begin(), m.end(), keys.begin(), boost::bind(&std::pair<std::string,Test>::first, _1)); } It seems to barf on the transform call because of inability to call get_pointer on std::pair<>?? From what I understand of transform though it should be behaving more like the former call (output to stream) that compiles and works: *out_it = op(*in_it) - in fact a test function works. Why is it even attempting to call get_pointer()? Boost version is 1.37.0, compiler is MSVC 8.0 Error output follows: 1>e:\boostvs3\include\boost\mem_fn.hpp(333) : error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'const std::pair<_Ty1,_Ty2>' 1> with 1> [ 1> _Ty1=const std::string, 1> _Ty2=Test 1> ] 1> e:\boostvs3\include\boost\get_pointer.hpp(21) : see declaration of 'boost::get_pointer' 1> e:\boostvs3\include\boost\mem_fn.hpp(352) : see reference to function template instantiation 'const R &boost::_mfi::dm<R,T>::call<const U>(U &,const void *) const' being compiled 1> with 1> [ 1> R=std::string, 1> T=std::pair<std::string,Test>, 1> U=std::pair<const std::string,Test> 1> ] 1> e:\boostvs3\include\boost\bind.hpp(222) : see reference to function template instantiation 'const R &boost::_mfi::dm<R,T>::operator ()<A1>(const U &) const' being compiled 1> with 1> [ 1> R=std::string, 1> T=std::pair<std::string,Test>, 1> A1=std::pair<const std::string,Test>, 1> U=std::pair<const std::string,Test> 1> ] 1> e:\boostvs3\include\boost\bind\bind_template.hpp(32) : see reference to function template instantiation 'R boost::_bi::list1<A1>::operator ()<const std::basic_string<_Elem,_Traits,_Ax>&,F,boost::_bi::list1<std::pair<_Ty1,_Ty2> &>>(boost::_bi::type<T>,F &,A &,long)' being compiled 1> with 1> [ 1> R=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, 1> A1=boost::arg<1>, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Ax=std::allocator<char>, 1> F=boost::_mfi::dm<std::string,std::pair<std::string,Test>>, 1> _Ty1=const std::string, 1> _Ty2=Test, 1> T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, 1> A=boost::_bi::list1<std::pair<const std::string,Test> &> 1> ] 1> c:\program files\microsoft visual studio 8\vc\include\algorithm(650) : see reference to function template instantiation 'const std::basic_string<_Elem,_Traits,_Ax> &boost::_bi::bind_t<R,F,L>::operator ()<std::pair<_Ty1,_Ty2>>(A1 &)' being compiled 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Ax=std::allocator<char>, 1> R=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, 1> F=boost::_mfi::dm<std::string,std::pair<std::string,Test>>, 1> L=boost::_bi::list1<boost::arg<1>>, 1> _Ty1=const std::string, 1> _Ty2=Test, 1> A1=std::pair<const std::string,Test> 1> ] 1> c:\program files\microsoft visual studio 8\vc\include\algorithm(685) : see reference to function template instantiation '_OutIt std::_Transform<std::_Tree<_Traits>::iterator,_OutIt,_Fn1,std::_Iter_random_helper<_Cat1,_Cat2>::_Iter_random_cat>(_InIt,_InIt,_OutIt,_Fn1,_InOutItCat,std::_Range_checked_iterator_tag)' being compiled 1> with 1> [ 1> _OutIt=std::_Vector_iterator<std::string,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>, 1> _Traits=std::_Tmap_traits<std::string,Test,std::less<std::string>,std::allocator<std::pair<const std::string,Test>>,false>, 1> _Fn1=boost::_bi::bind_t<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,boost::_mfi::dm<std::string,std::pair<std::string,Test>>,boost::_bi::list1<boost::arg<1>>>, 1> _Cat1=std::_Tree<std::_Tmap_traits<std::string,Test,std::less<std::string>,std::allocator<std::pair<const std::string,Test>>,false>>::iterator::iterator_category, 1> _Cat2=std::_Vector_iterator<std::string,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>::iterator_category, 1> _InIt=std::_Tree<std::_Tmap_traits<std::string,Test,std::less<std::string>,std::allocator<std::pair<const std::string,Test>>,false>>::iterator, 1> _InOutItCat=std::_Iter_random_helper<std::_Tree<std::_Tmap_traits<std::string,Test,std::less<std::string>,std::allocator<std::pair<const std::string,Test>>,false>>::iterator::iterator_category,std::_Vector_iterator<std::string,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>::iterator_category>::_Iter_random_cat 1> ] 1> e:\dev_workspace\experimental\scratch\scratch\main.cpp(21) : see reference to function template instantiation '_OutIt std::transform<std::_Tree<_Traits>::iterator,std::_Vector_iterator<_Ty,_Alloc>,boost::_bi::bind_t<R,F,L>>(_InIt,_InIt,_OutIt,_Fn1)' being compiled 1> with 1> [ 1> _OutIt=std::_Vector_iterator<std::string,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>, 1> _Traits=std::_Tmap_traits<std::string,Test,std::less<std::string>,std::allocator<std::pair<const std::string,Test>>,false>, 1> _Ty=std::string, 1> _Alloc=std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>, 1> R=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, 1> F=boost::_mfi::dm<std::string,std::pair<std::string,Test>>, 1> L=boost::_bi::list1<boost::arg<1>>, 1> _InIt=std::_Tree<std::_Tmap_traits<std::string,Test,std::less<std::string>,std::allocator<std::pair<const std::string,Test>>,false>>::iterator, 1> _Fn1=boost::_bi::bind_t<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,boost::_mfi::dm<std::string,std::pair<std::string,Test>>,boost::_bi::list1<boost::arg<1>>> 1> ] 1>e:\boostvs3\include\boost\mem_fn.hpp(333) : error C2784: 'T *boost::get_pointer(T *)' : could not deduce template argument for 'T *' from 'const std::pair<_Ty1,_Ty2>' 1> with 1> [ 1> _Ty1=const std::string, 1> _Ty2=Test 1> ] 1> e:\boostvs3\include\boost\get_pointer.hpp(14) : see declaration of 'boost::get_pointer'

Noah Roberts:
// won't compile: std::transform(m.begin(), m.end(), keys.begin(), boost::bind(&std::pair<std::string,Test>::first, _1));
The value type of the map m is pair<string const, Test> - note the const. Use the value_type typedef: typedef std::map<std::string, Test> map_type; std::transform( m.begin(), m.end(), keys.begin(), boost::bind(&map_type::value_type::first, _1) ); ...
1> T=std::pair<std::string,Test>, 1> U=std::pair<const std::string,Test>

Peter Dimov wrote:
Noah Roberts:
// won't compile: std::transform(m.begin(), m.end(), keys.begin(), boost::bind(&std::pair<std::string,Test>::first, _1));
The value type of the map m is pair<string const, Test> - note the const. Use the value_type typedef:
typedef std::map<std::string, Test> map_type;
std::transform( m.begin(), m.end(), keys.begin(), boost::bind(&map_type::value_type::first, _1) );
It would be something basic. Thanks.

// won't compile: std::transform(m.begin(), m.end(), keys.begin(), boost::bind(&std::pair<std::string,Test>::first, _1));
Try adding a const to the key type: &std::pair<const std::string,Test>::first I think the keys in a map are always returned const. Changing them could lead to unsorted maps and weird behaviour. Regards, Alex
participants (3)
-
Alex MDC
-
Noah Roberts
-
Peter Dimov