How to access n-th value of an integer_sequence? [duplicate]
up vote
4
down vote
favorite
This question already has an answer here:
template parameter packs access Nth type and Nth element
5 answers
I would like to know how to access the n-th value of an std::integer_sequence. For example given a type
using foo = std::integer_sequence<int, 3, 1, 4>;
I would like to have something like
auto i = get<foo, 2>(); // i = 4
Is there something in the standard library to do that? If not, do I need to resort to an iterative solution if I want this to work in C++14 (not C++17) ?
c++ c++14
marked as duplicate by Baum mit Augen
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
4
down vote
favorite
This question already has an answer here:
template parameter packs access Nth type and Nth element
5 answers
I would like to know how to access the n-th value of an std::integer_sequence. For example given a type
using foo = std::integer_sequence<int, 3, 1, 4>;
I would like to have something like
auto i = get<foo, 2>(); // i = 4
Is there something in the standard library to do that? If not, do I need to resort to an iterative solution if I want this to work in C++14 (not C++17) ?
c++ c++14
marked as duplicate by Baum mit Augen
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That's not really possible. What is the original problem you want to solve with this integer sequence? Why do you need to use it? Why do you need to get an arbitrary "element" from it?
– Some programmer dude
Nov 9 at 10:29
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
This question already has an answer here:
template parameter packs access Nth type and Nth element
5 answers
I would like to know how to access the n-th value of an std::integer_sequence. For example given a type
using foo = std::integer_sequence<int, 3, 1, 4>;
I would like to have something like
auto i = get<foo, 2>(); // i = 4
Is there something in the standard library to do that? If not, do I need to resort to an iterative solution if I want this to work in C++14 (not C++17) ?
c++ c++14
This question already has an answer here:
template parameter packs access Nth type and Nth element
5 answers
I would like to know how to access the n-th value of an std::integer_sequence. For example given a type
using foo = std::integer_sequence<int, 3, 1, 4>;
I would like to have something like
auto i = get<foo, 2>(); // i = 4
Is there something in the standard library to do that? If not, do I need to resort to an iterative solution if I want this to work in C++14 (not C++17) ?
This question already has an answer here:
template parameter packs access Nth type and Nth element
5 answers
c++ c++14
c++ c++14
asked Nov 9 at 10:24
user209974
9018
9018
marked as duplicate by Baum mit Augen
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Baum mit Augen
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That's not really possible. What is the original problem you want to solve with this integer sequence? Why do you need to use it? Why do you need to get an arbitrary "element" from it?
– Some programmer dude
Nov 9 at 10:29
add a comment |
That's not really possible. What is the original problem you want to solve with this integer sequence? Why do you need to use it? Why do you need to get an arbitrary "element" from it?
– Some programmer dude
Nov 9 at 10:29
That's not really possible. What is the original problem you want to solve with this integer sequence? Why do you need to use it? Why do you need to get an arbitrary "element" from it?
– Some programmer dude
Nov 9 at 10:29
That's not really possible. What is the original problem you want to solve with this integer sequence? Why do you need to use it? Why do you need to get an arbitrary "element" from it?
– Some programmer dude
Nov 9 at 10:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
13
down vote
accepted
There is no such built-in method as far as I'm aware but you can implement it itself in a few neat lines without any iterations:
template<class T, T... Ints>
constexpr T get(std::integer_sequence<T, Ints...>, std::size_t i) {
constexpr T arr = {Ints...};
return arr[i];
}
See how it works here: https://godbolt.org/z/yAfMeg
Arguments can be lifted into template parameters (to match your example) with a bit more code.
Isn'tconstexpr T arr = {Ints...};a C++17 feature?
– user209974
Nov 9 at 14:36
@user209974 no. By itself it's just C++11. But since the function is not a singlereturnstatement, it required C++14. On provided link you can see that all major compilers accept it just fine in C++14 mode.
– Dan M.
Nov 9 at 15:23
@user209974 don't forget to accept the answers for your questions (I see you have a lot of questions with no accepted answers).
– Dan M.
Nov 11 at 14:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
accepted
There is no such built-in method as far as I'm aware but you can implement it itself in a few neat lines without any iterations:
template<class T, T... Ints>
constexpr T get(std::integer_sequence<T, Ints...>, std::size_t i) {
constexpr T arr = {Ints...};
return arr[i];
}
See how it works here: https://godbolt.org/z/yAfMeg
Arguments can be lifted into template parameters (to match your example) with a bit more code.
Isn'tconstexpr T arr = {Ints...};a C++17 feature?
– user209974
Nov 9 at 14:36
@user209974 no. By itself it's just C++11. But since the function is not a singlereturnstatement, it required C++14. On provided link you can see that all major compilers accept it just fine in C++14 mode.
– Dan M.
Nov 9 at 15:23
@user209974 don't forget to accept the answers for your questions (I see you have a lot of questions with no accepted answers).
– Dan M.
Nov 11 at 14:59
add a comment |
up vote
13
down vote
accepted
There is no such built-in method as far as I'm aware but you can implement it itself in a few neat lines without any iterations:
template<class T, T... Ints>
constexpr T get(std::integer_sequence<T, Ints...>, std::size_t i) {
constexpr T arr = {Ints...};
return arr[i];
}
See how it works here: https://godbolt.org/z/yAfMeg
Arguments can be lifted into template parameters (to match your example) with a bit more code.
Isn'tconstexpr T arr = {Ints...};a C++17 feature?
– user209974
Nov 9 at 14:36
@user209974 no. By itself it's just C++11. But since the function is not a singlereturnstatement, it required C++14. On provided link you can see that all major compilers accept it just fine in C++14 mode.
– Dan M.
Nov 9 at 15:23
@user209974 don't forget to accept the answers for your questions (I see you have a lot of questions with no accepted answers).
– Dan M.
Nov 11 at 14:59
add a comment |
up vote
13
down vote
accepted
up vote
13
down vote
accepted
There is no such built-in method as far as I'm aware but you can implement it itself in a few neat lines without any iterations:
template<class T, T... Ints>
constexpr T get(std::integer_sequence<T, Ints...>, std::size_t i) {
constexpr T arr = {Ints...};
return arr[i];
}
See how it works here: https://godbolt.org/z/yAfMeg
Arguments can be lifted into template parameters (to match your example) with a bit more code.
There is no such built-in method as far as I'm aware but you can implement it itself in a few neat lines without any iterations:
template<class T, T... Ints>
constexpr T get(std::integer_sequence<T, Ints...>, std::size_t i) {
constexpr T arr = {Ints...};
return arr[i];
}
See how it works here: https://godbolt.org/z/yAfMeg
Arguments can be lifted into template parameters (to match your example) with a bit more code.
edited Nov 9 at 15:23
answered Nov 9 at 10:39
Dan M.
1,91511123
1,91511123
Isn'tconstexpr T arr = {Ints...};a C++17 feature?
– user209974
Nov 9 at 14:36
@user209974 no. By itself it's just C++11. But since the function is not a singlereturnstatement, it required C++14. On provided link you can see that all major compilers accept it just fine in C++14 mode.
– Dan M.
Nov 9 at 15:23
@user209974 don't forget to accept the answers for your questions (I see you have a lot of questions with no accepted answers).
– Dan M.
Nov 11 at 14:59
add a comment |
Isn'tconstexpr T arr = {Ints...};a C++17 feature?
– user209974
Nov 9 at 14:36
@user209974 no. By itself it's just C++11. But since the function is not a singlereturnstatement, it required C++14. On provided link you can see that all major compilers accept it just fine in C++14 mode.
– Dan M.
Nov 9 at 15:23
@user209974 don't forget to accept the answers for your questions (I see you have a lot of questions with no accepted answers).
– Dan M.
Nov 11 at 14:59
Isn't
constexpr T arr = {Ints...}; a C++17 feature?– user209974
Nov 9 at 14:36
Isn't
constexpr T arr = {Ints...}; a C++17 feature?– user209974
Nov 9 at 14:36
@user209974 no. By itself it's just C++11. But since the function is not a single
return statement, it required C++14. On provided link you can see that all major compilers accept it just fine in C++14 mode.– Dan M.
Nov 9 at 15:23
@user209974 no. By itself it's just C++11. But since the function is not a single
return statement, it required C++14. On provided link you can see that all major compilers accept it just fine in C++14 mode.– Dan M.
Nov 9 at 15:23
@user209974 don't forget to accept the answers for your questions (I see you have a lot of questions with no accepted answers).
– Dan M.
Nov 11 at 14:59
@user209974 don't forget to accept the answers for your questions (I see you have a lot of questions with no accepted answers).
– Dan M.
Nov 11 at 14:59
add a comment |
That's not really possible. What is the original problem you want to solve with this integer sequence? Why do you need to use it? Why do you need to get an arbitrary "element" from it?
– Some programmer dude
Nov 9 at 10:29