Created
September 20, 2014 10:20
-
-
Save Oberon00/a13b45a3f9ada48dc9dd to your computer and use it in GitHub Desktop.
Print arguments with indices (custom lower bound).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| // Integer sequence adapted from http://stackoverflow.com/a/17426611/2128694 | |
| // Just added the lower bound Lo. | |
| template<class T> using Invoke = typename T::type; | |
| template<unsigned...> struct seq{ using type = seq; }; | |
| template<class S1, class S2> struct concat; | |
| template<unsigned... I1, unsigned... I2> | |
| struct concat<seq<I1...>, seq<I2...>> | |
| : seq<I1..., (sizeof...(I1)+I2)...>{}; | |
| template<class S1, class S2> | |
| using Concat = Invoke<concat<S1, S2>>; | |
| template<unsigned N, unsigned Lo> struct gen_seq; | |
| template<unsigned N, unsigned Lo = 0> using GenSeq = Invoke<gen_seq<N, Lo>>; | |
| template<unsigned N, unsigned Lo> | |
| struct gen_seq : Concat<GenSeq<N/2, Lo>, GenSeq<N - N/2, Lo>>{}; | |
| template<unsigned Lo> struct gen_seq<0, Lo> : seq<>{ | |
| static_assert(Lo <= 0, "gen_seq: illegal 0 reach"); | |
| }; | |
| template<unsigned Lo> struct gen_seq<1, Lo> : seq<Lo>{}; | |
| struct VariadicPass { | |
| template <typename... Ts> | |
| VariadicPass(Ts&&...) {} | |
| }; | |
| template <unsigned... Is, typename... Ts> | |
| void print_impl(seq<Is...>, Ts&&... args) | |
| { | |
| // Note: Should be evaluated left to right but isn't on | |
| // MSVC (at least until 12/2013) and gcc < 4.9.1. | |
| // http://connect.microsoft.com/VisualStudio/feedbackdetail/view/976911 | |
| // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51253 | |
| VariadicPass {(std::cout << Is << ':' << args << ';')...}; | |
| } | |
| template <typename... Ts> | |
| void indexed_print(Ts&&... args) | |
| { | |
| print_impl(GenSeq<sizeof...(Ts), 5>(), std::forward<Ts>(args)...); | |
| } | |
| int main() | |
| { | |
| indexed_print(42, "abc"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment