Difference between BOOST_FOREACH and c++11 for range based loop?












12
















  1. What are the main differences between BOOST_FOREACH and c++11 range based loop?

  2. Is there a specific situation where I would want to use BOOST_FOREACH instead of range based loop or vice versa?


After executing a little test with std::vector filled with 1,000,000 int variables I found out that BOOST_FOREACH is a little bit slower than range based loop (took about 1.25 times longer than for a ranged based loop).










share|improve this question

























  • I'm not sure that there is a use for boost::foreach in new code anymore. I think it's only useful in old code, before there were ranged-based loops.

    – Mooing Duck
    Nov 19 '14 at 21:50











  • Do you mean BOOST_FOREACH?

    – Barry
    Nov 19 '14 at 21:52











  • @Barry Yes, I do. Let me edit that.

    – Laurynas Lazauskas
    Nov 19 '14 at 21:53


















12
















  1. What are the main differences between BOOST_FOREACH and c++11 range based loop?

  2. Is there a specific situation where I would want to use BOOST_FOREACH instead of range based loop or vice versa?


After executing a little test with std::vector filled with 1,000,000 int variables I found out that BOOST_FOREACH is a little bit slower than range based loop (took about 1.25 times longer than for a ranged based loop).










share|improve this question

























  • I'm not sure that there is a use for boost::foreach in new code anymore. I think it's only useful in old code, before there were ranged-based loops.

    – Mooing Duck
    Nov 19 '14 at 21:50











  • Do you mean BOOST_FOREACH?

    – Barry
    Nov 19 '14 at 21:52











  • @Barry Yes, I do. Let me edit that.

    – Laurynas Lazauskas
    Nov 19 '14 at 21:53
















12












12








12


2







  1. What are the main differences between BOOST_FOREACH and c++11 range based loop?

  2. Is there a specific situation where I would want to use BOOST_FOREACH instead of range based loop or vice versa?


After executing a little test with std::vector filled with 1,000,000 int variables I found out that BOOST_FOREACH is a little bit slower than range based loop (took about 1.25 times longer than for a ranged based loop).










share|improve this question

















  1. What are the main differences between BOOST_FOREACH and c++11 range based loop?

  2. Is there a specific situation where I would want to use BOOST_FOREACH instead of range based loop or vice versa?


After executing a little test with std::vector filled with 1,000,000 int variables I found out that BOOST_FOREACH is a little bit slower than range based loop (took about 1.25 times longer than for a ranged based loop).







c++ c++11 boost foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '14 at 21:54







Laurynas Lazauskas

















asked Nov 19 '14 at 21:47









Laurynas LazauskasLaurynas Lazauskas

6921916




6921916













  • I'm not sure that there is a use for boost::foreach in new code anymore. I think it's only useful in old code, before there were ranged-based loops.

    – Mooing Duck
    Nov 19 '14 at 21:50











  • Do you mean BOOST_FOREACH?

    – Barry
    Nov 19 '14 at 21:52











  • @Barry Yes, I do. Let me edit that.

    – Laurynas Lazauskas
    Nov 19 '14 at 21:53





















  • I'm not sure that there is a use for boost::foreach in new code anymore. I think it's only useful in old code, before there were ranged-based loops.

    – Mooing Duck
    Nov 19 '14 at 21:50











  • Do you mean BOOST_FOREACH?

    – Barry
    Nov 19 '14 at 21:52











  • @Barry Yes, I do. Let me edit that.

    – Laurynas Lazauskas
    Nov 19 '14 at 21:53



















I'm not sure that there is a use for boost::foreach in new code anymore. I think it's only useful in old code, before there were ranged-based loops.

– Mooing Duck
Nov 19 '14 at 21:50





I'm not sure that there is a use for boost::foreach in new code anymore. I think it's only useful in old code, before there were ranged-based loops.

– Mooing Duck
Nov 19 '14 at 21:50













Do you mean BOOST_FOREACH?

– Barry
Nov 19 '14 at 21:52





Do you mean BOOST_FOREACH?

– Barry
Nov 19 '14 at 21:52













@Barry Yes, I do. Let me edit that.

– Laurynas Lazauskas
Nov 19 '14 at 21:53







@Barry Yes, I do. Let me edit that.

– Laurynas Lazauskas
Nov 19 '14 at 21:53














2 Answers
2






active

oldest

votes


















20














The main difference is that range-for is a language construct, while BOOST_FOREACH is a macro doing lots of magic under the hood to do something that looks like that language construct. It is trying to do exactly the same thing with the limitations of pre-C++11. The goal of BOOST_FOREACH is range-for.



There is exactly one situation where I would even think of using BOOST_FOREACH instead of range-for, and it is iterating over a container of tuples where you want to unroll the tuple:



std::map<int, int> m;
int key, value;
BOOST_FOREACH(boost::tie(key, value), m)
{
// do something with key and value here
}


as compared to:



int key, value;
for (const auto& pair : m)
{
std::tie(key, value) = pair;
// do something
}


I like that you can put the tie directly into the loop header, although ultimately that's such a minor advantage that it's hardly worth even considering this as being a decision. Use range-for. Always.





C++17 will introduce structured bindings, which remove even that minor syntactical advantage:



for (auto const& [key, value] : m)
{
// do something
}


At that point, there will be no reason whatsoever to use BOOST_FOREACH.






share|improve this answer


























  • Nice trick with std::tie and boost_foreach :)

    – Viktor Sehr
    Nov 19 '18 at 16:08



















0














boost offers BOOST_REVERSE_FOREACH which allows you to traverse containers backwards. The for ranged loop does not provide an similar functionality.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27027652%2fdifference-between-boost-foreach-and-c11-for-range-based-loop%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    20














    The main difference is that range-for is a language construct, while BOOST_FOREACH is a macro doing lots of magic under the hood to do something that looks like that language construct. It is trying to do exactly the same thing with the limitations of pre-C++11. The goal of BOOST_FOREACH is range-for.



    There is exactly one situation where I would even think of using BOOST_FOREACH instead of range-for, and it is iterating over a container of tuples where you want to unroll the tuple:



    std::map<int, int> m;
    int key, value;
    BOOST_FOREACH(boost::tie(key, value), m)
    {
    // do something with key and value here
    }


    as compared to:



    int key, value;
    for (const auto& pair : m)
    {
    std::tie(key, value) = pair;
    // do something
    }


    I like that you can put the tie directly into the loop header, although ultimately that's such a minor advantage that it's hardly worth even considering this as being a decision. Use range-for. Always.





    C++17 will introduce structured bindings, which remove even that minor syntactical advantage:



    for (auto const& [key, value] : m)
    {
    // do something
    }


    At that point, there will be no reason whatsoever to use BOOST_FOREACH.






    share|improve this answer


























    • Nice trick with std::tie and boost_foreach :)

      – Viktor Sehr
      Nov 19 '18 at 16:08
















    20














    The main difference is that range-for is a language construct, while BOOST_FOREACH is a macro doing lots of magic under the hood to do something that looks like that language construct. It is trying to do exactly the same thing with the limitations of pre-C++11. The goal of BOOST_FOREACH is range-for.



    There is exactly one situation where I would even think of using BOOST_FOREACH instead of range-for, and it is iterating over a container of tuples where you want to unroll the tuple:



    std::map<int, int> m;
    int key, value;
    BOOST_FOREACH(boost::tie(key, value), m)
    {
    // do something with key and value here
    }


    as compared to:



    int key, value;
    for (const auto& pair : m)
    {
    std::tie(key, value) = pair;
    // do something
    }


    I like that you can put the tie directly into the loop header, although ultimately that's such a minor advantage that it's hardly worth even considering this as being a decision. Use range-for. Always.





    C++17 will introduce structured bindings, which remove even that minor syntactical advantage:



    for (auto const& [key, value] : m)
    {
    // do something
    }


    At that point, there will be no reason whatsoever to use BOOST_FOREACH.






    share|improve this answer


























    • Nice trick with std::tie and boost_foreach :)

      – Viktor Sehr
      Nov 19 '18 at 16:08














    20












    20








    20







    The main difference is that range-for is a language construct, while BOOST_FOREACH is a macro doing lots of magic under the hood to do something that looks like that language construct. It is trying to do exactly the same thing with the limitations of pre-C++11. The goal of BOOST_FOREACH is range-for.



    There is exactly one situation where I would even think of using BOOST_FOREACH instead of range-for, and it is iterating over a container of tuples where you want to unroll the tuple:



    std::map<int, int> m;
    int key, value;
    BOOST_FOREACH(boost::tie(key, value), m)
    {
    // do something with key and value here
    }


    as compared to:



    int key, value;
    for (const auto& pair : m)
    {
    std::tie(key, value) = pair;
    // do something
    }


    I like that you can put the tie directly into the loop header, although ultimately that's such a minor advantage that it's hardly worth even considering this as being a decision. Use range-for. Always.





    C++17 will introduce structured bindings, which remove even that minor syntactical advantage:



    for (auto const& [key, value] : m)
    {
    // do something
    }


    At that point, there will be no reason whatsoever to use BOOST_FOREACH.






    share|improve this answer















    The main difference is that range-for is a language construct, while BOOST_FOREACH is a macro doing lots of magic under the hood to do something that looks like that language construct. It is trying to do exactly the same thing with the limitations of pre-C++11. The goal of BOOST_FOREACH is range-for.



    There is exactly one situation where I would even think of using BOOST_FOREACH instead of range-for, and it is iterating over a container of tuples where you want to unroll the tuple:



    std::map<int, int> m;
    int key, value;
    BOOST_FOREACH(boost::tie(key, value), m)
    {
    // do something with key and value here
    }


    as compared to:



    int key, value;
    for (const auto& pair : m)
    {
    std::tie(key, value) = pair;
    // do something
    }


    I like that you can put the tie directly into the loop header, although ultimately that's such a minor advantage that it's hardly worth even considering this as being a decision. Use range-for. Always.





    C++17 will introduce structured bindings, which remove even that minor syntactical advantage:



    for (auto const& [key, value] : m)
    {
    // do something
    }


    At that point, there will be no reason whatsoever to use BOOST_FOREACH.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 9 '17 at 8:53









    Rakete1111

    34.6k1082118




    34.6k1082118










    answered Nov 19 '14 at 21:58









    BarryBarry

    181k19315576




    181k19315576













    • Nice trick with std::tie and boost_foreach :)

      – Viktor Sehr
      Nov 19 '18 at 16:08



















    • Nice trick with std::tie and boost_foreach :)

      – Viktor Sehr
      Nov 19 '18 at 16:08

















    Nice trick with std::tie and boost_foreach :)

    – Viktor Sehr
    Nov 19 '18 at 16:08





    Nice trick with std::tie and boost_foreach :)

    – Viktor Sehr
    Nov 19 '18 at 16:08













    0














    boost offers BOOST_REVERSE_FOREACH which allows you to traverse containers backwards. The for ranged loop does not provide an similar functionality.






    share|improve this answer




























      0














      boost offers BOOST_REVERSE_FOREACH which allows you to traverse containers backwards. The for ranged loop does not provide an similar functionality.






      share|improve this answer


























        0












        0








        0







        boost offers BOOST_REVERSE_FOREACH which allows you to traverse containers backwards. The for ranged loop does not provide an similar functionality.






        share|improve this answer













        boost offers BOOST_REVERSE_FOREACH which allows you to traverse containers backwards. The for ranged loop does not provide an similar functionality.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 15:35









        CatrielCatriel

        14




        14






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27027652%2fdifference-between-boost-foreach-and-c11-for-range-based-loop%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)