Sadly, I cannot use any of stl/std libraries from C++, because I am programming for a embedded Operating System which only has available gcc 4.4.4
with bare C++, so, no std::tuple
, std::forward
, std::apply
or std::anything_else
.
To help understand meta generic generated code, I am presenting a minimal example code compiled with clang because it has a option to show us the generated template-meta-programming/metaprogramming code.
This question is just for curiosity because instead of generating the integer parameter pack in the wrong order, I can just create it on a correct order. This is what I use to generate my integer packager pack on the wrong order:
template<int ...>
struct MetaSequenceOfIntegers { };
template<int AccumulatedSize, typename Tn, int... GeneratedSequence>
struct GeneratorOfIntegerSequence;
template<int AccumulatedSize, typename Grouper, typename Head, typename... Tail, int... GeneratedSequence>
struct GeneratorOfIntegerSequence< AccumulatedSize, Grouper( Head, Tail... ), GeneratedSequence... >
{
typedef typename GeneratorOfIntegerSequence
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), AccumulatedSize, GeneratedSequence...
>::type type;
};
template<int AccumulatedSize, typename Grouper, int... GeneratedSequence>
struct GeneratorOfIntegerSequence<AccumulatedSize, Grouper(), GeneratedSequence...>
{
typedef MetaSequenceOfIntegers<GeneratedSequence...> type;
};
template<int ...Sequence>
void intergers_sequencer_generator(MetaSequenceOfIntegers<Sequence...>) {
int array[] = {Sequence...};
}
int main(int argc, char const *argv[]) {
intergers_sequencer_generator( GeneratorOfIntegerSequence< 0, int(char, int, char) >::type() );
return 0;
}
I am only using int array[] = {Sequence...}
for demonstration. The real code used is like this:
template<typename ReturnType, typename... Tn>
class Closure
{
// ... other code
template<int ...Sequence>
ReturnType _run(MetaSequenceOfIntegers<Sequence...>) {
return _function_entry_pointer( get_nth_function_argument_on_address<Sequence, Tn>()... );
}
// ... other code
}
Which generates this bellow, for an input like create_functor( &function1, 'a', 10, 'b' )
:
template <int ...Sequence> char _run(MetaSequenceOfIntegers<Sequence...>);
template<> char _run<<5, 1, 0>>(MetaSequenceOfIntegers<5, 1, 0>) {
return this->_function_entry_pointer(
this->get_nth_function_argument_on_address<5, const char *>(),
this->get_nth_function_argument_on_address<1, const char *>(),
this->get_nth_function_argument_on_address<0, char>()
);
}
// and much more
We can see the generated code using clang:
$ clang++ -Xclang -ast-print -fsyntax-only generator.cpp > expanded.cpp
template <int ...> struct MetaSequenceOfIntegers {
};
template<> struct MetaSequenceOfIntegers<<5, 1, 0>> {
};
template <int AccumulatedSize, typename Tn, int ...GeneratedSequence> struct GeneratorOfIntegerSequence
template<> struct GeneratorOfIntegerSequence<0, int (char, int, char), <>> {
typedef typename GeneratorOfIntegerSequence<0 + sizeof(char), int (int, char), 0>::type type;
}
template<> struct GeneratorOfIntegerSequence<1, int (int, char), <0>> {
typedef typename GeneratorOfIntegerSequence<1 + sizeof(int), int (char), 1, 0>::type type;
}
template<> struct GeneratorOfIntegerSequence<5, int (char), <1, 0>> {
typedef typename GeneratorOfIntegerSequence<5 + sizeof(char), int (), 5, 1, 0>::type type;
}
template<> struct GeneratorOfIntegerSequence<6, int (), <5, 1, 0>> {
typedef MetaSequenceOfIntegers<5, 1, 0> type;
};
template <int AccumulatedSize, typename Grouper, typename Head, typename ...Tail, int ...GeneratedSequence> struct GeneratorOfIntegerSequence<AccumulatedSize, type-parameter-0-1 (type-parameter-0-2, type-parameter-0-3...), <GeneratedSequence...>> {
typedef typename GeneratorOfIntegerSequence<AccumulatedSize + sizeof(Head), Grouper (Tail...), AccumulatedSize, GeneratedSequence...>::type type;
};
template <int AccumulatedSize, typename Grouper, int ...GeneratedSequence> struct GeneratorOfIntegerSequence<AccumulatedSize, type-parameter-0-1 (), <GeneratedSequence...>> {
typedef MetaSequenceOfIntegers<GeneratedSequence...> type;
};
template <int ...Sequence> void intergers_sequencer_generator(MetaSequenceOfIntegers<Sequence...>) {
int array[] = {Sequence...};
}
template<> void intergers_sequencer_generator<<5, 1, 0>>(MetaSequenceOfIntegers<5, 1, 0>) {
int array[] = {5, 1, 0};
}
int main(int argc, const char *argv[]) {
intergers_sequencer_generator(GeneratorOfIntegerSequence<0, int (char, int, char)>::type());
return 0;
}
The meta programming list is generated is on reversed order I need it to be. Instead of int array[] = {5, 1, 0}
, it should be int array[] = {0, 1, 5}
.
I managed to generated the list on the correct order just changing this line on in example code:
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), GeneratedSequence..., AccumulatedSize
// to -->
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), AccumulatedSize, GeneratedSequence...
But let us supposed I could not do that because the list is input from a third part which I do not have control. How can I reverse the parameter pack <5, 1, 0>
to <0, 1, 5>
without using any std library functions?
In my first attempt, I tried to use the same strategy as the one I did to generate the integer list, but I could not make it to compile:
template<int ...>
struct MetaSequenceOfIntegers { };
template<int AccumulatedSize, typename Tn, int... GeneratedSequence>
struct GeneratorOfIntegerSequence;
template<int AccumulatedSize, typename Grouper, typename Head, typename... Tail, int... GeneratedSequence>
struct GeneratorOfIntegerSequence< AccumulatedSize, Grouper( Head, Tail... ), GeneratedSequence... >
{
typedef typename GeneratorOfIntegerSequence
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), AccumulatedSize, GeneratedSequence...
>::type type;
};
template<int AccumulatedSize, typename Grouper, int... GeneratedSequence>
struct GeneratorOfIntegerSequence<AccumulatedSize, Grouper(), GeneratedSequence...>
{
typedef MetaSequenceOfIntegers<GeneratedSequence...> type;
};
// The new code starts here
template<int ...>
struct MetaSequenceReversed { };
template<typename Tn, int... GeneratedSequence>
struct ReversorOfIntegerSequence;
template<typename Grouper, int Head, int... Tail, int... GeneratedSequence>
struct ReversorOfIntegerSequence< Grouper( Head, Tail... ), GeneratedSequence... >
{
typedef typename ReversorOfIntegerSequence
< Grouper( Tail... ), GeneratedSequence...
>::type type;
};
template<typename Grouper, int... GeneratedSequence>
struct ReversorOfIntegerSequence<Grouper(), GeneratedSequence...>
{
typedef MetaSequenceReversed<GeneratedSequence...> type;
};
template<int ...ReversedSequence>
void intergers_sequencer_reversor(MetaSequenceReversed<ReversedSequence...>) {
int reversed_array[] = {ReversedSequence...};
}
template<int ...Sequence>
void intergers_sequencer_generator(MetaSequenceOfIntegers<Sequence...>) {
int array[] = {Sequence...};
intergers_sequencer_reversor( ReversorOfIntegerSequence< int(Sequence...) >::type() );
}
int main(int argc, char const *argv[])
{
intergers_sequencer_generator( GeneratorOfIntegerSequence< 0, int(char, int, char) >::type() );
return 0;
}
When I try to build this, I got this error:
generator.cpp:29:35: error: template argument for template type parameter must be a type
struct ReversorOfIntegerSequence< Grouper( Head, Tail... ), GeneratedSequence... >
^~~~~~~~~~~~~~~~~~~~~~~~
generator.cpp:25:19: note: template parameter is declared here
template<typename Tn, int... GeneratedSequence>
^
generator.cpp:50:62: error: template argument for template type parameter must be a type
intergers_sequencer_reversor( ReversorOfIntegerSequence< int(Sequence...) >::type() );
^~~~~~~~~~~~~~~~
generator.cpp:25:19: note: template parameter is declared here
template<typename Tn, int... GeneratedSequence>
^
References:
- Variadic templates, parameter pack and its discussed ambiguity in a parameter list
- "unpacking" a tuple to call a matching function pointer
- Can we see the template instantiated code by C++ compiler
- Build function parameters with variadic templates
- How to reverse the order of arguments of a variadic template function?
Not sure to understand what do you exactly can use but... it seems easy to me.
Given an helper struct as follows
the revert struct can be simply
I suppose that a reverse function can be useful
I propose a modified version of your original code with addition of reversed sequence (using also
std::cout
to print the sequences, but you can remove it, obviously).