What is a concise notation for a range based for loop iterating over a few tuples
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
add a comment |
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
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
add a comment |
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
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
c++ c++17
asked Nov 23 '18 at 10:18
Paul Jurczak
3,0112445
3,0112445
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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);
Beautiful! Since I usually dispense withstd::
, 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
|
show 1 more comment
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.
2
Oh. And there I was mucking around with classes and deduction guides.
– Quentin
Nov 23 '18 at 10:35
add a comment |
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. :)
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
add a comment |
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);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
Beautiful! Since I usually dispense withstd::
, 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
|
show 1 more comment
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);
Beautiful! Since I usually dispense withstd::
, 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
|
show 1 more comment
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);
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);
edited Nov 23 '18 at 15:14
answered Nov 23 '18 at 10:39
Jarod42
113k12101181
113k12101181
Beautiful! Since I usually dispense withstd::
, 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
|
show 1 more comment
Beautiful! Since I usually dispense withstd::
, 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
|
show 1 more comment
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.
2
Oh. And there I was mucking around with classes and deduction guides.
– Quentin
Nov 23 '18 at 10:35
add a comment |
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.
2
Oh. And there I was mucking around with classes and deduction guides.
– Quentin
Nov 23 '18 at 10:35
add a comment |
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.
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.
answered Nov 23 '18 at 10:34
rubenvb
52.8k22136257
52.8k22136257
2
Oh. And there I was mucking around with classes and deduction guides.
– Quentin
Nov 23 '18 at 10:35
add a comment |
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
add a comment |
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. :)
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
add a comment |
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. :)
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
add a comment |
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. :)
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. :)
edited Nov 23 '18 at 10:37
answered Nov 23 '18 at 10:33
P.W
11.4k3842
11.4k3842
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
add a comment |
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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 23 '18 at 10:34
lubgr
10.3k21745
10.3k21745
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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