c++ reversed integer sequence implementation

ぐ巨炮叔叔 提交于 2020-01-23 12:07:54

问题


Who knows how to implement C++ std::make_index_sequence reverse version. To get - make_index_sequence_reverse<int, 5> = <4,3,2,1,0>. Thank you!


回答1:


IMHO, there is no reason for a index_sequence_reverse: std::index_sequence support sequences of indexes and are order neutral (or even without order).

If you can use std::make_index_sequence, for a makeIndexSequenceReverse you can make something as follows

#include <utility>
#include <type_traits>

template <std::size_t ... Is>
constexpr auto indexSequenceReverse (std::index_sequence<Is...> const &)
   -> decltype( std::index_sequence<sizeof...(Is)-1U-Is...>{} );

template <std::size_t N>
using makeIndexSequenceReverse
   = decltype(indexSequenceReverse(std::make_index_sequence<N>{}));

int main ()
 {
   static_assert( std::is_same<std::index_sequence<4U, 3U, 2U, 1U, 0U>,
      makeIndexSequenceReverse<5U>>::value, "!" );
 }


来源:https://stackoverflow.com/questions/51408771/c-reversed-integer-sequence-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!