Using BOOST_FOREACH with a constant intrusive list

我只是一个虾纸丫 提交于 2019-12-11 03:12:50

问题


Consider the following code to iterate over an intrusive list using the BOOST_FOREACH macro:

#include <boost/foreach.hpp>
#include <boost/intrusive/list.hpp>

typedef boost::intrusive::list<
    boost::intrusive::list_base_hook<> > MyList;

void iterate (const MyList& xs) {
    BOOST_FOREACH (MyList::const_reference node, xs);
}

int main () {
    MyList xs;
    iterate (xs);
    return 0;
}

Given boost version 1.48 the code fails with clang 3.2 (SVN) and gcc 4.6.3, but works with gcc 4.5.3. With non-const-qualified parameter xs to iterate the code works. With C++11 enabled all of the compilers accept the code. When using boost-1.46 both gcc versions accept the code, but clang still doesn't.

Is the code at hand a misuse of the BOOST_FOREACH macro, or is the error at boosts side? Is there a workaround that is nicer than iteration with regular for-loop?

Edit: I pasted the error messages to pastebin (both are very verbose) for GCC and clang.


回答1:


Here is what I could gather from the logs as well as my deductions as to the cause of failure.

Short version: for some reason BOOST_FOREACH attempts to copy the data which is not possible.

There is a note on the Extensibility page:

Making BOOST_FOREACH Work with Non-Copyable Sequence Types

For sequence types that are non-copyable, we will need to tell BOOST_FOREACH to not try to make copies. If our type inherits from boost::noncopyable, no further action is required. If not, we must specialize the boost::foreach::is_noncopyable<> template [...] Another way to achieve the same effect is to override the global boost_foreach_is_noncopyable() function. Doing it this way has the advantage of being portable to older compilers.

From the diagnosis, it is unclear whether the type is properly configured, so you might want to give it a go.


Pruned diagnosis and analysis.

/usr/include/boost/foreach.hpp:571:37: error: no matching constructor for initialization of 'boost::intrusive::list< >'
        ::new(this->data.address()) T(t);
                                    ^ ~
/usr/include/boost/foreach.hpp:648:51: note: in instantiation of member function 'boost::foreach_detail_::simple_variant<boost::intrusive::list< > >::simple_variant' requested here
    return auto_any<simple_variant<T> >(*rvalue ? simple_variant<T>(t) : simple_variant<T>(&t));
                                                  ^

/usr/include/boost/intrusive/list.hpp:1490:35: note: candidate constructor not viable: 1st argument ('const boost::intrusive::list< >') would lose const qualifier
   BOOST_MOVABLE_BUT_NOT_COPYABLE(list)
                                  ^
/usr/include/boost/move/move.hpp:371:7: note: expanded from macro 'BOOST_MOVABLE_BUT_NOT_COPYABLE'
      TYPE(TYPE &);\

/usr/include/boost/intrusive/list.hpp:1497:4: note: candidate constructor not viable: no known conversion from 'const boost::intrusive::list< >' to 'const value_traits' (aka 'const boost::intrusive::detail::base_hook_traits<boost::intrusive::list_base_hook< >, boost::intrusive::list_node_traits<void *>, 1, boost::intrusive::default_tag, 1>') for 1st argument; 
   list(const value_traits &v_traits = value_traits())
   ^
/usr/include/boost/intrusive/list.hpp:1506:4: note: candidate constructor not viable: no known conversion from 'const boost::intrusive::list< >' to '::boost::rv<list< >> &' for 1st argument; 
   list(BOOST_RV_REF(list) x)
   ^
/usr/include/boost/intrusive/list.hpp:1502:4: note: candidate constructor template not viable: requires at least 2 arguments, but 1 was provided
   list(Iterator b, Iterator e, const value_traits &v_traits = value_traits())
   ^

I tried to isolate the error as much as possible (removing backtraces etc..) Apparently the problem stems from boost::intrusive::list, and more precisely the inability to build a new boost::intrusive::list<> from a boost::intrusive::list<> const.

The most promising constructor is defined by a macro:

BOOST_MOVABLE_BUT_NOT_COPYABLE(list)

which expands to

list(list&);

which is the way boost emulates move semantics for non-copyable types in C++03. However it cannot move from a const item since the const qualifier would be lost.

This looks to be part of the trickery used by BOOST_FOREACH to avoid multiple evaluation of the container argument (in case it is a function invocation) though I am a little surprised it tries to copy the argument here.




回答2:


Since you are using gcc > 4.6 and clang 3.2, you could use C++11's range-based for looops:

#include <boost/foreach.hpp>
#include <boost/intrusive/list.hpp>

typedef boost::intrusive::list<
    boost::intrusive::list_base_hook<> > MyList;

void iterate (const MyList& xs) {
    for(const auto &node : xs) {
        // do something with node
    }
}

int main () {
    MyList xs;
    iterate (xs);
    return 0;
}

You could also use std::for_each:

void iterate (const MyList& xs) {
    std::for_each(xs.begin(), xs.end(), 
      [](MyList::const_reference node) {
         // do something with node
      }
    );
}


来源:https://stackoverflow.com/questions/10836399/using-boost-foreach-with-a-constant-intrusive-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!