Why did the range based 'for' loop specification change in C++17? [duplicate]How the new range-based for loop in C++17 helps Ranges TS?What are the rules about using an underscore in a C++ identifier?What exactly do “IB” and “UB” mean?How to use range-based for() loop with std::map?C++11 reverse range-based for-loopWhy are elementwise additions much faster in separate loops than in a combined loop?Why does changing 0.1f to 0 slow down performance by 10x?Why is my program slow when looping over exactly 8192 elements?How the new range-based for loop in C++17 helps Ranges TS?Using Auto based Ranged for loop vs using pair based Ranged for looprange based for used for primitive typeRange based for in C++17 for custom container or general classes with different begin/end types

Setting up uniform distribution over non-rectangular support

Can a weapon be "unsheathable"?

Why is Leela so good at beating Stockfish in the French/Caro-Kann?

A short story (possibility written in the 80's), where humans visit an alien race that evolves fast?

Postdoc Fellowships Collision

How did 36-bit computers format ARPANET packets?

Does Special Relativity Imply Multiple Realities?

Is it possible to conserve the total kinetic energy of a system, but not its momentum?

What would be the effect(s) of this asteroid?

Should I provide my username and password for my brokerage account to my mortgage lender to verify my assets?

What does “critical but stable” mean?

How to be ready for The Rise of Skywalker?

Left-right non-bimodule examples

When does one use an extra stave for piano music?

Understanding of big-O massively improved when I began thinking of orders as sets. How to apply the same approach to big-Theta?

Fill your bucket with 2020

Missing node when using Tikz package

Fill the Golfeek gutter

How do I change the shortcut icon back? v2.81

How to optimize the following function?

How would I measure the Carbon Dioxide content in Coca Cola over a period of time?

Limit number of matches of find command

Where is Bing's Picture of the Day stored on my system?

damage triggering a simultaneous modifier of damage



Why did the range based 'for' loop specification change in C++17? [duplicate]


How the new range-based for loop in C++17 helps Ranges TS?What are the rules about using an underscore in a C++ identifier?What exactly do “IB” and “UB” mean?How to use range-based for() loop with std::map?C++11 reverse range-based for-loopWhy are elementwise additions much faster in separate loops than in a combined loop?Why does changing 0.1f to 0 slow down performance by 10x?Why is my program slow when looping over exactly 8192 elements?How the new range-based for loop in C++17 helps Ranges TS?Using Auto based Ranged for loop vs using pair based Ranged for looprange based for used for primitive typeRange based for in C++17 for custom container or general classes with different begin/end types






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









60


















I was looking over some ugly code (that was modifying the underlying sequence while iterating), and to explore the definition of the range-based for loop, I went to cppreference.



There I noticed something strange:



The range based for loop changed in C++17, but I do not see the reason for the change, and the code looks the same to me (just "refactored").
So the old one was:




auto && __range = range_expression;
for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




The new one is




auto && __range = range_expression;
auto __begin = begin_expr;
auto __end = end_expr;
for ( ; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




Why was this change made, and does it make any legal C++14 programs exhibit undefined behavior (UB) in C++17?










share|improve this question



























  • @r3musn0x I think this is probably a broader question than the one you’ve linked to.

    – Tim
    Oct 3 at 19:02






  • 1





    @Tim, I agree, however the accepted answer to that question is way more comprehensive and it covers both of them.

    – r3mus n0x
    Oct 3 at 19:11






  • 1





    As an aside, it's against the C++ standard to name your locals with double underscores. stackoverflow.com/a/228797

    – Max Barraclough
    Oct 5 at 14:59

















60


















I was looking over some ugly code (that was modifying the underlying sequence while iterating), and to explore the definition of the range-based for loop, I went to cppreference.



There I noticed something strange:



The range based for loop changed in C++17, but I do not see the reason for the change, and the code looks the same to me (just "refactored").
So the old one was:




auto && __range = range_expression;
for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




The new one is




auto && __range = range_expression;
auto __begin = begin_expr;
auto __end = end_expr;
for ( ; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




Why was this change made, and does it make any legal C++14 programs exhibit undefined behavior (UB) in C++17?










share|improve this question



























  • @r3musn0x I think this is probably a broader question than the one you’ve linked to.

    – Tim
    Oct 3 at 19:02






  • 1





    @Tim, I agree, however the accepted answer to that question is way more comprehensive and it covers both of them.

    – r3mus n0x
    Oct 3 at 19:11






  • 1





    As an aside, it's against the C++ standard to name your locals with double underscores. stackoverflow.com/a/228797

    – Max Barraclough
    Oct 5 at 14:59













60













60









60


2






I was looking over some ugly code (that was modifying the underlying sequence while iterating), and to explore the definition of the range-based for loop, I went to cppreference.



There I noticed something strange:



The range based for loop changed in C++17, but I do not see the reason for the change, and the code looks the same to me (just "refactored").
So the old one was:




auto && __range = range_expression;
for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




The new one is




auto && __range = range_expression;
auto __begin = begin_expr;
auto __end = end_expr;
for ( ; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




Why was this change made, and does it make any legal C++14 programs exhibit undefined behavior (UB) in C++17?










share|improve this question
















I was looking over some ugly code (that was modifying the underlying sequence while iterating), and to explore the definition of the range-based for loop, I went to cppreference.



There I noticed something strange:



The range based for loop changed in C++17, but I do not see the reason for the change, and the code looks the same to me (just "refactored").
So the old one was:




auto && __range = range_expression;
for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




The new one is




auto && __range = range_expression;
auto __begin = begin_expr;
auto __end = end_expr;
for ( ; __begin != __end; ++__begin)
range_declaration = *__begin;
loop_statement




Why was this change made, and does it make any legal C++14 programs exhibit undefined behavior (UB) in C++17?








This question already has answers here:





How the new range-based for loop in C++17 helps Ranges TS?

(2 answers)


Closed 3 months ago.






This question already has answers here:







This question already has answers here:





This question already has answers here:




How the new range-based for loop in C++17 helps Ranges TS?

(2 answers)



Closed 3 months ago.





c++ for-loop language-lawyer c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 4 at 17:18









Peter Mortensen

24.4k19 gold badges89 silver badges118 bronze badges




24.4k19 gold badges89 silver badges118 bronze badges










asked Oct 2 at 19:50









NoSenseEtAlNoSenseEtAl

18.8k18 gold badges83 silver badges200 bronze badges




18.8k18 gold badges83 silver badges200 bronze badges















  • @r3musn0x I think this is probably a broader question than the one you’ve linked to.

    – Tim
    Oct 3 at 19:02






  • 1





    @Tim, I agree, however the accepted answer to that question is way more comprehensive and it covers both of them.

    – r3mus n0x
    Oct 3 at 19:11






  • 1





    As an aside, it's against the C++ standard to name your locals with double underscores. stackoverflow.com/a/228797

    – Max Barraclough
    Oct 5 at 14:59

















  • @r3musn0x I think this is probably a broader question than the one you’ve linked to.

    – Tim
    Oct 3 at 19:02






  • 1





    @Tim, I agree, however the accepted answer to that question is way more comprehensive and it covers both of them.

    – r3mus n0x
    Oct 3 at 19:11






  • 1





    As an aside, it's against the C++ standard to name your locals with double underscores. stackoverflow.com/a/228797

    – Max Barraclough
    Oct 5 at 14:59
















@r3musn0x I think this is probably a broader question than the one you’ve linked to.

– Tim
Oct 3 at 19:02





@r3musn0x I think this is probably a broader question than the one you’ve linked to.

– Tim
Oct 3 at 19:02




1




1





@Tim, I agree, however the accepted answer to that question is way more comprehensive and it covers both of them.

– r3mus n0x
Oct 3 at 19:11





@Tim, I agree, however the accepted answer to that question is way more comprehensive and it covers both of them.

– r3mus n0x
Oct 3 at 19:11




1




1





As an aside, it's against the C++ standard to name your locals with double underscores. stackoverflow.com/a/228797

– Max Barraclough
Oct 5 at 14:59





As an aside, it's against the C++ standard to name your locals with double underscores. stackoverflow.com/a/228797

– Max Barraclough
Oct 5 at 14:59












2 Answers
2






active

oldest

votes


















79



















Using



auto __begin = begin_expr, __end = end_expr;


requires both begin_expr and end_expr to return the same type. This means you cannot have a sentinel iterator type that is different from the beginning type. Using



auto __begin = begin_expr ;
auto __end = end_expr ;


fixes that issue while proving full backwards compatibility with C++14.






share|improve this answer



























  • It would be more logical to have this update together with Ranges, probably. Iterator-sentinel ranges are still very restricted in C++17.

    – L. F.
    Oct 3 at 5:33







  • 2





    @L.F. IIRC ranges was originally slated to be in C++17, instead we got a TS since it wasn't quite ready. We still need this if we want to use the ranges TS, or ranges-v3.

    – NathanOliver- Reinstate Monica
    Oct 3 at 12:30






  • 2





    @L.F. Ranges are a library feature. There is range-v3 lib on github that served as the basis for standardization and it benefited from this standardiese change.

    – Dan M.
    Oct 3 at 13:12







  • 1





    Including the feature in C++17 proper also allows programmers to implement their own sentinels (or similar) if desired, and can potentially allow bugs to be discovered before the associated library additions are ready, @L.F.

    – Justin Time - Reinstate Monica
    Oct 3 at 18:30


















31



















It is explained later in the "notes":




As of C++17, the types of the begin_expr and the end_expr do not have to be the same ...




and you can't have that with:



auto __begin = begin_expr, __end = end_expr;





share|improve this answer
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    79



















    Using



    auto __begin = begin_expr, __end = end_expr;


    requires both begin_expr and end_expr to return the same type. This means you cannot have a sentinel iterator type that is different from the beginning type. Using



    auto __begin = begin_expr ;
    auto __end = end_expr ;


    fixes that issue while proving full backwards compatibility with C++14.






    share|improve this answer



























    • It would be more logical to have this update together with Ranges, probably. Iterator-sentinel ranges are still very restricted in C++17.

      – L. F.
      Oct 3 at 5:33







    • 2





      @L.F. IIRC ranges was originally slated to be in C++17, instead we got a TS since it wasn't quite ready. We still need this if we want to use the ranges TS, or ranges-v3.

      – NathanOliver- Reinstate Monica
      Oct 3 at 12:30






    • 2





      @L.F. Ranges are a library feature. There is range-v3 lib on github that served as the basis for standardization and it benefited from this standardiese change.

      – Dan M.
      Oct 3 at 13:12







    • 1





      Including the feature in C++17 proper also allows programmers to implement their own sentinels (or similar) if desired, and can potentially allow bugs to be discovered before the associated library additions are ready, @L.F.

      – Justin Time - Reinstate Monica
      Oct 3 at 18:30















    79



















    Using



    auto __begin = begin_expr, __end = end_expr;


    requires both begin_expr and end_expr to return the same type. This means you cannot have a sentinel iterator type that is different from the beginning type. Using



    auto __begin = begin_expr ;
    auto __end = end_expr ;


    fixes that issue while proving full backwards compatibility with C++14.






    share|improve this answer



























    • It would be more logical to have this update together with Ranges, probably. Iterator-sentinel ranges are still very restricted in C++17.

      – L. F.
      Oct 3 at 5:33







    • 2





      @L.F. IIRC ranges was originally slated to be in C++17, instead we got a TS since it wasn't quite ready. We still need this if we want to use the ranges TS, or ranges-v3.

      – NathanOliver- Reinstate Monica
      Oct 3 at 12:30






    • 2





      @L.F. Ranges are a library feature. There is range-v3 lib on github that served as the basis for standardization and it benefited from this standardiese change.

      – Dan M.
      Oct 3 at 13:12







    • 1





      Including the feature in C++17 proper also allows programmers to implement their own sentinels (or similar) if desired, and can potentially allow bugs to be discovered before the associated library additions are ready, @L.F.

      – Justin Time - Reinstate Monica
      Oct 3 at 18:30













    79















    79











    79









    Using



    auto __begin = begin_expr, __end = end_expr;


    requires both begin_expr and end_expr to return the same type. This means you cannot have a sentinel iterator type that is different from the beginning type. Using



    auto __begin = begin_expr ;
    auto __end = end_expr ;


    fixes that issue while proving full backwards compatibility with C++14.






    share|improve this answer
















    Using



    auto __begin = begin_expr, __end = end_expr;


    requires both begin_expr and end_expr to return the same type. This means you cannot have a sentinel iterator type that is different from the beginning type. Using



    auto __begin = begin_expr ;
    auto __end = end_expr ;


    fixes that issue while proving full backwards compatibility with C++14.







    share|improve this answer















    share|improve this answer




    share|improve this answer








    edited Oct 2 at 20:23

























    answered Oct 2 at 19:57









    NathanOliver- Reinstate MonicaNathanOliver- Reinstate Monica

    124k19 gold badges197 silver badges280 bronze badges




    124k19 gold badges197 silver badges280 bronze badges















    • It would be more logical to have this update together with Ranges, probably. Iterator-sentinel ranges are still very restricted in C++17.

      – L. F.
      Oct 3 at 5:33







    • 2





      @L.F. IIRC ranges was originally slated to be in C++17, instead we got a TS since it wasn't quite ready. We still need this if we want to use the ranges TS, or ranges-v3.

      – NathanOliver- Reinstate Monica
      Oct 3 at 12:30






    • 2





      @L.F. Ranges are a library feature. There is range-v3 lib on github that served as the basis for standardization and it benefited from this standardiese change.

      – Dan M.
      Oct 3 at 13:12







    • 1





      Including the feature in C++17 proper also allows programmers to implement their own sentinels (or similar) if desired, and can potentially allow bugs to be discovered before the associated library additions are ready, @L.F.

      – Justin Time - Reinstate Monica
      Oct 3 at 18:30

















    • It would be more logical to have this update together with Ranges, probably. Iterator-sentinel ranges are still very restricted in C++17.

      – L. F.
      Oct 3 at 5:33







    • 2





      @L.F. IIRC ranges was originally slated to be in C++17, instead we got a TS since it wasn't quite ready. We still need this if we want to use the ranges TS, or ranges-v3.

      – NathanOliver- Reinstate Monica
      Oct 3 at 12:30






    • 2





      @L.F. Ranges are a library feature. There is range-v3 lib on github that served as the basis for standardization and it benefited from this standardiese change.

      – Dan M.
      Oct 3 at 13:12







    • 1





      Including the feature in C++17 proper also allows programmers to implement their own sentinels (or similar) if desired, and can potentially allow bugs to be discovered before the associated library additions are ready, @L.F.

      – Justin Time - Reinstate Monica
      Oct 3 at 18:30
















    It would be more logical to have this update together with Ranges, probably. Iterator-sentinel ranges are still very restricted in C++17.

    – L. F.
    Oct 3 at 5:33






    It would be more logical to have this update together with Ranges, probably. Iterator-sentinel ranges are still very restricted in C++17.

    – L. F.
    Oct 3 at 5:33





    2




    2





    @L.F. IIRC ranges was originally slated to be in C++17, instead we got a TS since it wasn't quite ready. We still need this if we want to use the ranges TS, or ranges-v3.

    – NathanOliver- Reinstate Monica
    Oct 3 at 12:30





    @L.F. IIRC ranges was originally slated to be in C++17, instead we got a TS since it wasn't quite ready. We still need this if we want to use the ranges TS, or ranges-v3.

    – NathanOliver- Reinstate Monica
    Oct 3 at 12:30




    2




    2





    @L.F. Ranges are a library feature. There is range-v3 lib on github that served as the basis for standardization and it benefited from this standardiese change.

    – Dan M.
    Oct 3 at 13:12






    @L.F. Ranges are a library feature. There is range-v3 lib on github that served as the basis for standardization and it benefited from this standardiese change.

    – Dan M.
    Oct 3 at 13:12





    1




    1





    Including the feature in C++17 proper also allows programmers to implement their own sentinels (or similar) if desired, and can potentially allow bugs to be discovered before the associated library additions are ready, @L.F.

    – Justin Time - Reinstate Monica
    Oct 3 at 18:30





    Including the feature in C++17 proper also allows programmers to implement their own sentinels (or similar) if desired, and can potentially allow bugs to be discovered before the associated library additions are ready, @L.F.

    – Justin Time - Reinstate Monica
    Oct 3 at 18:30













    31



















    It is explained later in the "notes":




    As of C++17, the types of the begin_expr and the end_expr do not have to be the same ...




    and you can't have that with:



    auto __begin = begin_expr, __end = end_expr;





    share|improve this answer





























      31



















      It is explained later in the "notes":




      As of C++17, the types of the begin_expr and the end_expr do not have to be the same ...




      and you can't have that with:



      auto __begin = begin_expr, __end = end_expr;





      share|improve this answer



























        31















        31











        31









        It is explained later in the "notes":




        As of C++17, the types of the begin_expr and the end_expr do not have to be the same ...




        and you can't have that with:



        auto __begin = begin_expr, __end = end_expr;





        share|improve this answer














        It is explained later in the "notes":




        As of C++17, the types of the begin_expr and the end_expr do not have to be the same ...




        and you can't have that with:



        auto __begin = begin_expr, __end = end_expr;






        share|improve this answer













        share|improve this answer




        share|improve this answer










        answered Oct 2 at 20:02









        danadamdanadam

        2,01515 silver badges14 bronze badges




        2,01515 silver badges14 bronze badges
















            Popular posts from this blog

            Tamil (spriik) Luke uk diar | Nawigatjuun

            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

            Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?