How to access n-th value of an integer_sequence? [duplicate]











up vote
4
down vote

favorite
1













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) ?










share|improve this question













marked as duplicate by Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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















up vote
4
down vote

favorite
1













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) ?










share|improve this question













marked as duplicate by Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1






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) ?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 10:24









user209974

9018




9018




marked as duplicate by Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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


















  • 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












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.






share|improve this answer























  • 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 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


















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.






share|improve this answer























  • 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 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















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.






share|improve this answer























  • 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 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 15:23

























answered Nov 9 at 10:39









Dan M.

1,91511123




1,91511123












  • 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 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










  • @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
















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



Popular posts from this blog

Landwehr

Reims

Schenkenzell