What is a concise notation for a range based for loop iterating over a few tuples












3















Is there a simpler (shorter) way to write this code snippet as a loop:



for (auto [a, b]: {pair<int, int>{1, 2}, pair<int, int>{3, 4}})
foo(a, b);


Preferably making the initializer list as close as possible to this form, which doesn't compile:



for (auto [a, b]: {{1, 2}, {3, 4}})
foo(a, b);









share|improve this question























  • cough macr cough o...

    – YSC
    Nov 23 '18 at 10:34






  • 4





    do { foo(1, 2); foo(3, 4); } while (false); ;-)

    – Jarod42
    Nov 23 '18 at 10:36













  • @Jarod42 You are legally correct! ;-)

    – Paul Jurczak
    Nov 23 '18 at 10:39
















3















Is there a simpler (shorter) way to write this code snippet as a loop:



for (auto [a, b]: {pair<int, int>{1, 2}, pair<int, int>{3, 4}})
foo(a, b);


Preferably making the initializer list as close as possible to this form, which doesn't compile:



for (auto [a, b]: {{1, 2}, {3, 4}})
foo(a, b);









share|improve this question























  • cough macr cough o...

    – YSC
    Nov 23 '18 at 10:34






  • 4





    do { foo(1, 2); foo(3, 4); } while (false); ;-)

    – Jarod42
    Nov 23 '18 at 10:36













  • @Jarod42 You are legally correct! ;-)

    – Paul Jurczak
    Nov 23 '18 at 10:39














3












3








3








Is there a simpler (shorter) way to write this code snippet as a loop:



for (auto [a, b]: {pair<int, int>{1, 2}, pair<int, int>{3, 4}})
foo(a, b);


Preferably making the initializer list as close as possible to this form, which doesn't compile:



for (auto [a, b]: {{1, 2}, {3, 4}})
foo(a, b);









share|improve this question














Is there a simpler (shorter) way to write this code snippet as a loop:



for (auto [a, b]: {pair<int, int>{1, 2}, pair<int, int>{3, 4}})
foo(a, b);


Preferably making the initializer list as close as possible to this form, which doesn't compile:



for (auto [a, b]: {{1, 2}, {3, 4}})
foo(a, b);






c++ c++17






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 10:18









Paul JurczakPaul Jurczak

3,0462646




3,0462646













  • cough macr cough o...

    – YSC
    Nov 23 '18 at 10:34






  • 4





    do { foo(1, 2); foo(3, 4); } while (false); ;-)

    – Jarod42
    Nov 23 '18 at 10:36













  • @Jarod42 You are legally correct! ;-)

    – Paul Jurczak
    Nov 23 '18 at 10:39



















  • cough macr cough o...

    – YSC
    Nov 23 '18 at 10:34






  • 4





    do { foo(1, 2); foo(3, 4); } while (false); ;-)

    – Jarod42
    Nov 23 '18 at 10:36













  • @Jarod42 You are legally correct! ;-)

    – Paul Jurczak
    Nov 23 '18 at 10:39

















cough macr cough o...

– YSC
Nov 23 '18 at 10:34





cough macr cough o...

– YSC
Nov 23 '18 at 10:34




4




4





do { foo(1, 2); foo(3, 4); } while (false); ;-)

– Jarod42
Nov 23 '18 at 10:36







do { foo(1, 2); foo(3, 4); } while (false); ;-)

– Jarod42
Nov 23 '18 at 10:36















@Jarod42 You are legally correct! ;-)

– Paul Jurczak
Nov 23 '18 at 10:39





@Jarod42 You are legally correct! ;-)

– Paul Jurczak
Nov 23 '18 at 10:39












4 Answers
4






active

oldest

votes


















4














You might do the following in C++17 thanks to deduction guide:



for (auto [a, b]: {std::pair{1, 2}, std::pair{3, 4}})
foo(a, b);


or even



for (auto [a, b]: {std::pair{1, 2}, {3, 4}})
foo(a, b);





share|improve this answer


























  • Beautiful! Since I usually dispense with std::, your solution is literally the most concise and flexible one.

    – Paul Jurczak
    Nov 23 '18 at 10:56











  • Along these lines: for (auto [i, j, k]: {tuple{.1, .5, 5}, tuple{.4, .5, 6}})

    – Paul Jurczak
    Nov 23 '18 at 11:13






  • 2





    @PaulJurczak - You can go one shorter. Only the first item must explicitly be a pair. The second one will be deduced based of the first.

    – StoryTeller
    Nov 23 '18 at 12:08











  • This scales terribly though...

    – rubenvb
    Nov 23 '18 at 15:03











  • @rubenvb What do you mean by scales terribly? Compile time penalty?

    – Paul Jurczak
    Nov 23 '18 at 21:32





















5














As C++ is a strongly typed language, there are limits to the fuzziness you can expect from the compiler deduced types. Two levels of unspecified braces is an example of this.



You can make it shorter by using a simple typedef:



using pair_list = std::initializer_list<std::pair<int, int>>;

for (auto [a, b]: pair_list{{1, 2}, {3, 4}})
foo(a, b);


Live demo here. You can optimize/choose the temporary's type as you wish.






share|improve this answer



















  • 2





    Oh. And there I was mucking around with classes and deduction guides.

    – Quentin
    Nov 23 '18 at 10:35



















2














There is a shorter way to do it which involves construction of a temporary std::map.



You can do something like this:



for (auto [a, b]: map<int, int>{{1, 2}, {3, 4}})
foo(a, b);


This compiles. See it here.



Or as @Jarod42 suggested, you can use std::initializer_list<std::pair<int, int>> as well.



for (auto [a, b]: std::initializer_list<std::pair<int, int>>{{1, 2}, {3, 4}})


You will have to type a bit more. :)






share|improve this answer


























  • std::initializer_list<std::pair<int, int>> should do the job too.

    – Jarod42
    Nov 23 '18 at 10:34











  • @Jarod42: Thanks. Updated.

    – P.W
    Nov 23 '18 at 10:37











  • map<int, int> looks good, but will probably be a performance killer in an inner loop.

    – Paul Jurczak
    Nov 23 '18 at 10:45











  • @PaulJurczak: Yeah, I was going for the shortest. :)

    – P.W
    Nov 23 '18 at 10:50



















1














Still a bit verbose, but this template might be of help:



template <class T>
constexpr auto asPairs(std::initializer_list<std::pair<T, T>> args)
{
return args;
}


It can be instantiated and used like the following.



for (auto [a, b] : asPairs<int>({{1, 2}, {3, 4}}))
foo(a, b);





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%2f53444764%2fwhat-is-a-concise-notation-for-a-range-based-for-loop-iterating-over-a-few-tuple%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    You might do the following in C++17 thanks to deduction guide:



    for (auto [a, b]: {std::pair{1, 2}, std::pair{3, 4}})
    foo(a, b);


    or even



    for (auto [a, b]: {std::pair{1, 2}, {3, 4}})
    foo(a, b);





    share|improve this answer


























    • Beautiful! Since I usually dispense with std::, your solution is literally the most concise and flexible one.

      – Paul Jurczak
      Nov 23 '18 at 10:56











    • Along these lines: for (auto [i, j, k]: {tuple{.1, .5, 5}, tuple{.4, .5, 6}})

      – Paul Jurczak
      Nov 23 '18 at 11:13






    • 2





      @PaulJurczak - You can go one shorter. Only the first item must explicitly be a pair. The second one will be deduced based of the first.

      – StoryTeller
      Nov 23 '18 at 12:08











    • This scales terribly though...

      – rubenvb
      Nov 23 '18 at 15:03











    • @rubenvb What do you mean by scales terribly? Compile time penalty?

      – Paul Jurczak
      Nov 23 '18 at 21:32


















    4














    You might do the following in C++17 thanks to deduction guide:



    for (auto [a, b]: {std::pair{1, 2}, std::pair{3, 4}})
    foo(a, b);


    or even



    for (auto [a, b]: {std::pair{1, 2}, {3, 4}})
    foo(a, b);





    share|improve this answer


























    • Beautiful! Since I usually dispense with std::, your solution is literally the most concise and flexible one.

      – Paul Jurczak
      Nov 23 '18 at 10:56











    • Along these lines: for (auto [i, j, k]: {tuple{.1, .5, 5}, tuple{.4, .5, 6}})

      – Paul Jurczak
      Nov 23 '18 at 11:13






    • 2





      @PaulJurczak - You can go one shorter. Only the first item must explicitly be a pair. The second one will be deduced based of the first.

      – StoryTeller
      Nov 23 '18 at 12:08











    • This scales terribly though...

      – rubenvb
      Nov 23 '18 at 15:03











    • @rubenvb What do you mean by scales terribly? Compile time penalty?

      – Paul Jurczak
      Nov 23 '18 at 21:32
















    4












    4








    4







    You might do the following in C++17 thanks to deduction guide:



    for (auto [a, b]: {std::pair{1, 2}, std::pair{3, 4}})
    foo(a, b);


    or even



    for (auto [a, b]: {std::pair{1, 2}, {3, 4}})
    foo(a, b);





    share|improve this answer















    You might do the following in C++17 thanks to deduction guide:



    for (auto [a, b]: {std::pair{1, 2}, std::pair{3, 4}})
    foo(a, b);


    or even



    for (auto [a, b]: {std::pair{1, 2}, {3, 4}})
    foo(a, b);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 15:14

























    answered Nov 23 '18 at 10:39









    Jarod42Jarod42

    119k12104189




    119k12104189













    • Beautiful! Since I usually dispense with std::, your solution is literally the most concise and flexible one.

      – Paul Jurczak
      Nov 23 '18 at 10:56











    • Along these lines: for (auto [i, j, k]: {tuple{.1, .5, 5}, tuple{.4, .5, 6}})

      – Paul Jurczak
      Nov 23 '18 at 11:13






    • 2





      @PaulJurczak - You can go one shorter. Only the first item must explicitly be a pair. The second one will be deduced based of the first.

      – StoryTeller
      Nov 23 '18 at 12:08











    • This scales terribly though...

      – rubenvb
      Nov 23 '18 at 15:03











    • @rubenvb What do you mean by scales terribly? Compile time penalty?

      – Paul Jurczak
      Nov 23 '18 at 21:32





















    • Beautiful! Since I usually dispense with std::, your solution is literally the most concise and flexible one.

      – Paul Jurczak
      Nov 23 '18 at 10:56











    • Along these lines: for (auto [i, j, k]: {tuple{.1, .5, 5}, tuple{.4, .5, 6}})

      – Paul Jurczak
      Nov 23 '18 at 11:13






    • 2





      @PaulJurczak - You can go one shorter. Only the first item must explicitly be a pair. The second one will be deduced based of the first.

      – StoryTeller
      Nov 23 '18 at 12:08











    • This scales terribly though...

      – rubenvb
      Nov 23 '18 at 15:03











    • @rubenvb What do you mean by scales terribly? Compile time penalty?

      – Paul Jurczak
      Nov 23 '18 at 21:32



















    Beautiful! Since I usually dispense with std::, your solution is literally the most concise and flexible one.

    – Paul Jurczak
    Nov 23 '18 at 10:56





    Beautiful! Since I usually dispense with std::, your solution is literally the most concise and flexible one.

    – Paul Jurczak
    Nov 23 '18 at 10:56













    Along these lines: for (auto [i, j, k]: {tuple{.1, .5, 5}, tuple{.4, .5, 6}})

    – Paul Jurczak
    Nov 23 '18 at 11:13





    Along these lines: for (auto [i, j, k]: {tuple{.1, .5, 5}, tuple{.4, .5, 6}})

    – Paul Jurczak
    Nov 23 '18 at 11:13




    2




    2





    @PaulJurczak - You can go one shorter. Only the first item must explicitly be a pair. The second one will be deduced based of the first.

    – StoryTeller
    Nov 23 '18 at 12:08





    @PaulJurczak - You can go one shorter. Only the first item must explicitly be a pair. The second one will be deduced based of the first.

    – StoryTeller
    Nov 23 '18 at 12:08













    This scales terribly though...

    – rubenvb
    Nov 23 '18 at 15:03





    This scales terribly though...

    – rubenvb
    Nov 23 '18 at 15:03













    @rubenvb What do you mean by scales terribly? Compile time penalty?

    – Paul Jurczak
    Nov 23 '18 at 21:32







    @rubenvb What do you mean by scales terribly? Compile time penalty?

    – Paul Jurczak
    Nov 23 '18 at 21:32















    5














    As C++ is a strongly typed language, there are limits to the fuzziness you can expect from the compiler deduced types. Two levels of unspecified braces is an example of this.



    You can make it shorter by using a simple typedef:



    using pair_list = std::initializer_list<std::pair<int, int>>;

    for (auto [a, b]: pair_list{{1, 2}, {3, 4}})
    foo(a, b);


    Live demo here. You can optimize/choose the temporary's type as you wish.






    share|improve this answer



















    • 2





      Oh. And there I was mucking around with classes and deduction guides.

      – Quentin
      Nov 23 '18 at 10:35
















    5














    As C++ is a strongly typed language, there are limits to the fuzziness you can expect from the compiler deduced types. Two levels of unspecified braces is an example of this.



    You can make it shorter by using a simple typedef:



    using pair_list = std::initializer_list<std::pair<int, int>>;

    for (auto [a, b]: pair_list{{1, 2}, {3, 4}})
    foo(a, b);


    Live demo here. You can optimize/choose the temporary's type as you wish.






    share|improve this answer



















    • 2





      Oh. And there I was mucking around with classes and deduction guides.

      – Quentin
      Nov 23 '18 at 10:35














    5












    5








    5







    As C++ is a strongly typed language, there are limits to the fuzziness you can expect from the compiler deduced types. Two levels of unspecified braces is an example of this.



    You can make it shorter by using a simple typedef:



    using pair_list = std::initializer_list<std::pair<int, int>>;

    for (auto [a, b]: pair_list{{1, 2}, {3, 4}})
    foo(a, b);


    Live demo here. You can optimize/choose the temporary's type as you wish.






    share|improve this answer













    As C++ is a strongly typed language, there are limits to the fuzziness you can expect from the compiler deduced types. Two levels of unspecified braces is an example of this.



    You can make it shorter by using a simple typedef:



    using pair_list = std::initializer_list<std::pair<int, int>>;

    for (auto [a, b]: pair_list{{1, 2}, {3, 4}})
    foo(a, b);


    Live demo here. You can optimize/choose the temporary's type as you wish.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 10:34









    rubenvbrubenvb

    54k22140261




    54k22140261








    • 2





      Oh. And there I was mucking around with classes and deduction guides.

      – Quentin
      Nov 23 '18 at 10:35














    • 2





      Oh. And there I was mucking around with classes and deduction guides.

      – Quentin
      Nov 23 '18 at 10:35








    2




    2





    Oh. And there I was mucking around with classes and deduction guides.

    – Quentin
    Nov 23 '18 at 10:35





    Oh. And there I was mucking around with classes and deduction guides.

    – Quentin
    Nov 23 '18 at 10:35











    2














    There is a shorter way to do it which involves construction of a temporary std::map.



    You can do something like this:



    for (auto [a, b]: map<int, int>{{1, 2}, {3, 4}})
    foo(a, b);


    This compiles. See it here.



    Or as @Jarod42 suggested, you can use std::initializer_list<std::pair<int, int>> as well.



    for (auto [a, b]: std::initializer_list<std::pair<int, int>>{{1, 2}, {3, 4}})


    You will have to type a bit more. :)






    share|improve this answer


























    • std::initializer_list<std::pair<int, int>> should do the job too.

      – Jarod42
      Nov 23 '18 at 10:34











    • @Jarod42: Thanks. Updated.

      – P.W
      Nov 23 '18 at 10:37











    • map<int, int> looks good, but will probably be a performance killer in an inner loop.

      – Paul Jurczak
      Nov 23 '18 at 10:45











    • @PaulJurczak: Yeah, I was going for the shortest. :)

      – P.W
      Nov 23 '18 at 10:50
















    2














    There is a shorter way to do it which involves construction of a temporary std::map.



    You can do something like this:



    for (auto [a, b]: map<int, int>{{1, 2}, {3, 4}})
    foo(a, b);


    This compiles. See it here.



    Or as @Jarod42 suggested, you can use std::initializer_list<std::pair<int, int>> as well.



    for (auto [a, b]: std::initializer_list<std::pair<int, int>>{{1, 2}, {3, 4}})


    You will have to type a bit more. :)






    share|improve this answer


























    • std::initializer_list<std::pair<int, int>> should do the job too.

      – Jarod42
      Nov 23 '18 at 10:34











    • @Jarod42: Thanks. Updated.

      – P.W
      Nov 23 '18 at 10:37











    • map<int, int> looks good, but will probably be a performance killer in an inner loop.

      – Paul Jurczak
      Nov 23 '18 at 10:45











    • @PaulJurczak: Yeah, I was going for the shortest. :)

      – P.W
      Nov 23 '18 at 10:50














    2












    2








    2







    There is a shorter way to do it which involves construction of a temporary std::map.



    You can do something like this:



    for (auto [a, b]: map<int, int>{{1, 2}, {3, 4}})
    foo(a, b);


    This compiles. See it here.



    Or as @Jarod42 suggested, you can use std::initializer_list<std::pair<int, int>> as well.



    for (auto [a, b]: std::initializer_list<std::pair<int, int>>{{1, 2}, {3, 4}})


    You will have to type a bit more. :)






    share|improve this answer















    There is a shorter way to do it which involves construction of a temporary std::map.



    You can do something like this:



    for (auto [a, b]: map<int, int>{{1, 2}, {3, 4}})
    foo(a, b);


    This compiles. See it here.



    Or as @Jarod42 suggested, you can use std::initializer_list<std::pair<int, int>> as well.



    for (auto [a, b]: std::initializer_list<std::pair<int, int>>{{1, 2}, {3, 4}})


    You will have to type a bit more. :)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 10:37

























    answered Nov 23 '18 at 10:33









    P.WP.W

    17.8k41657




    17.8k41657













    • std::initializer_list<std::pair<int, int>> should do the job too.

      – Jarod42
      Nov 23 '18 at 10:34











    • @Jarod42: Thanks. Updated.

      – P.W
      Nov 23 '18 at 10:37











    • map<int, int> looks good, but will probably be a performance killer in an inner loop.

      – Paul Jurczak
      Nov 23 '18 at 10:45











    • @PaulJurczak: Yeah, I was going for the shortest. :)

      – P.W
      Nov 23 '18 at 10:50



















    • std::initializer_list<std::pair<int, int>> should do the job too.

      – Jarod42
      Nov 23 '18 at 10:34











    • @Jarod42: Thanks. Updated.

      – P.W
      Nov 23 '18 at 10:37











    • map<int, int> looks good, but will probably be a performance killer in an inner loop.

      – Paul Jurczak
      Nov 23 '18 at 10:45











    • @PaulJurczak: Yeah, I was going for the shortest. :)

      – P.W
      Nov 23 '18 at 10:50

















    std::initializer_list<std::pair<int, int>> should do the job too.

    – Jarod42
    Nov 23 '18 at 10:34





    std::initializer_list<std::pair<int, int>> should do the job too.

    – Jarod42
    Nov 23 '18 at 10:34













    @Jarod42: Thanks. Updated.

    – P.W
    Nov 23 '18 at 10:37





    @Jarod42: Thanks. Updated.

    – P.W
    Nov 23 '18 at 10:37













    map<int, int> looks good, but will probably be a performance killer in an inner loop.

    – Paul Jurczak
    Nov 23 '18 at 10:45





    map<int, int> looks good, but will probably be a performance killer in an inner loop.

    – Paul Jurczak
    Nov 23 '18 at 10:45













    @PaulJurczak: Yeah, I was going for the shortest. :)

    – P.W
    Nov 23 '18 at 10:50





    @PaulJurczak: Yeah, I was going for the shortest. :)

    – P.W
    Nov 23 '18 at 10:50











    1














    Still a bit verbose, but this template might be of help:



    template <class T>
    constexpr auto asPairs(std::initializer_list<std::pair<T, T>> args)
    {
    return args;
    }


    It can be instantiated and used like the following.



    for (auto [a, b] : asPairs<int>({{1, 2}, {3, 4}}))
    foo(a, b);





    share|improve this answer




























      1














      Still a bit verbose, but this template might be of help:



      template <class T>
      constexpr auto asPairs(std::initializer_list<std::pair<T, T>> args)
      {
      return args;
      }


      It can be instantiated and used like the following.



      for (auto [a, b] : asPairs<int>({{1, 2}, {3, 4}}))
      foo(a, b);





      share|improve this answer


























        1












        1








        1







        Still a bit verbose, but this template might be of help:



        template <class T>
        constexpr auto asPairs(std::initializer_list<std::pair<T, T>> args)
        {
        return args;
        }


        It can be instantiated and used like the following.



        for (auto [a, b] : asPairs<int>({{1, 2}, {3, 4}}))
        foo(a, b);





        share|improve this answer













        Still a bit verbose, but this template might be of help:



        template <class T>
        constexpr auto asPairs(std::initializer_list<std::pair<T, T>> args)
        {
        return args;
        }


        It can be instantiated and used like the following.



        for (auto [a, b] : asPairs<int>({{1, 2}, {3, 4}}))
        foo(a, b);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 10:34









        lubgrlubgr

        14.7k32152




        14.7k32152






























            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%2f53444764%2fwhat-is-a-concise-notation-for-a-range-based-for-loop-iterating-over-a-few-tuple%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?