Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:41:11+00:00 2026-06-05T00:41:11+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:41:13+00:00Added an answer on June 5, 2026 at 12:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);
Consider the following code: #include <iostream> using namespace std; class Test { static int
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Consider the following code example: TempList.ForEach(Function(obj) obj.Deleted = True End Function) And this one:
Consider following code: typedef SomeType type_t[2]; SomeType * arr1 = new type_t; //new or
Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine(List of
Consider following code: enum size = 16; double[size] arr1 = [...]; double[size] arr2 =
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.