Boost::tuple - assigning to get<>()

387 views Asked by At

The following fails to compile on the assignment to t.get<1>().

struct pull_from_memory {
    pull_from_memory(MemoryBank &m) : m_map(m) {};
    void operator()(boost::tuple<string&, string&> &t ) {
        MemoryBank::iterator i;
        if ( ( i = m_map.find(t.get<0>())) != m_map.end() ) {
            t.get<1>() = *i;  // compilation error
        }
    }
private:
    MemoryBank &m_map;
};

The error message, FWIW, is:

tom.cpp: In member function 'void pull_from_memory::operator()(boost::tuples::tuple<std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>&)':
tom.cpp:424: error: no match for 'operator=' in '((boost::tuples::tuple<std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>*)t)->boost::tuples::tuple<std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>::<anonymous>.boost::tuples::cons<HT, TT>::get [with int N = 1, HT = std::string&, TT = boost::tuples::cons<std::string&, boost::tuples::null_type>]() = i.boost::unordered_detail::hash_iterator<A, G>::operator* [with A = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, G = boost::unordered_detail::ungrouped]()'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:510: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:518: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:529: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

UPDATE 1: after correcting for the fact that map.find returns a pair, I now get a link error, apparently related to instantiating the template

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h: In function '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = boost::zip_iterator<boost::tuples::tuple<__gnu_cxx::__normal_iterator<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, __gnu_cxx::__normal_iterator<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> >, _Funct = bind_from_memory]':
/mxhome/charrison/git/sdcore/libs/CommUtils/test/../include/Block.h:160:   instantiated from 'void Block::traverse_record(const D_RecordType&, Function) [with Function = bind_from_memory]'
tom.cpp:473:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:4200: error: no match for call to '(bind_from_memory) (boost::tuples::cons<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::tuples::cons<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::tuples::null_type> >)'
tom.cpp:460: note: candidates are: void bind_from_memory::operator()(boost::tuples::tuple<const std::string&, std::string&, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>&)
2

There are 2 answers

0
zindorsky On BEST ANSWER

Your problem is that you're expecting *i to be a std::string, but it isn't. It's a (reference to) std::pair<std::string,std::string>.

That's how iterators for maps work.

Try this instead:

t.get<1>() = i->second; //if you want the value corresponding to the key
2
Dietmar Kühl On

From the looks of it, you try to assign a std::pair<std::string const, std::string> to a std::string. This won't work. Did you mean to write this?

t.get<1>() = i->first;