如何反转整数参数包?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何反转整数参数包?相关的知识,希望对你有一定的参考价值。

遗憾的是,我不能使用C ++中的任何 / 库,因为我正在为嵌入式操作系统编程,只有可用的 4.4.4和裸C ++,所以,没有std::tuplestd::forwardstd::applystd::anything_else

为了帮助理解元通用生成代码,我提出了一个用编译的最小示例代码,因为它有一个选项向我们展示生成的 / 代码。

这个问题只是为了好奇,因为我可以按正确的顺序创建它,而不是以错误的顺序生成整数参数包。这是我用于在错误的顺序上生成我的整数打包器包:

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;
}

我只使用int array[] = {Sequence...}进行演示。使用的真实代码是这样的:

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
}

对于像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

我们可以使用查看生成的代码:

$ 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;
}

生成的元编程列表是按照我需要的相反顺序生成的。而不是int array[] = {5, 1, 0},它应该是int array[] = {0, 1, 5}

我设法在正确的顺序上生成列表,只是在示例代码中更改此行:

< AccumulatedSize + sizeof(Head), Grouper( Tail... ), GeneratedSequence..., AccumulatedSize
// to -->
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), AccumulatedSize, GeneratedSequence...

但是让我们假设我不能这样做,因为列表是从我无法控制的第三部分输入的。如何在不使用任何std库函数的情况下将参数包<5, 1, 0>反转为<0, 1, 5>

在我的第一次尝试中,我尝试使用与生成整数列表相同的策略,但我无法编译:

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;
}

当我尝试构建它时,我收到此错误:

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

参考文献:

  1. Variadic templates, parameter pack and its discussed ambiguity in a parameter list
  2. "unpacking" a tuple to call a matching function pointer
  3. Can we see the template instantiated code by C++ compiler
  4. Build function parameters with variadic templates
  5. How to reverse the order of arguments of a variadic template function?
答案

如何在不使用任何std库函数的情况下将参数pack <5, 1, 0>反转为<0, 1, 5>

不知道你究竟能用什么,但......对我来说似乎很容易。

给出一个辅助结构如下

template <typename, typename>
struct RS_helper;

template <int ... As, int B0, int ... Bs>
struct RS_helper<MetaSequenceOfIntegers<As...>,
                 MetaSequenceOfIntegers<B0, Bs...>>
   : RS_helper<MetaSequenceOfIntegers<B0, As...>,
               MetaSequenceOfIntegers<Bs...>>
 { };

template <typename T>
struct RS_helper<T, MetaSequenceOfIntegers<>>
 { using type = T; };

恢复结构可以很简单

template <int ... Is>
struct RevertSequence
   : RS_helper<MetaSequenceOfIntegers<>, MetaSequenceOfIntegers<Is...>>
 { };

我认为反向功能可能很有用

template <int ... Is>
constexpr typename RevertSequence<Is...>::type
   revertSequenceFunction (MetaSequenceOfIntegers<Is...> const &)
 { return {}; }

我提出了原始代码的修改版本,并添加了反向序列(使用std::cout来打印序列,但显然你可以删除它)。

#include <iostream>

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...>)
 {
   using unused = int[];

   (void)unused { 0, (std::cout << Sequence << ' ', 0)... };

   std::cout << std::endl;
 }


template <typename, typename>
struct RS_helper;

template <int ... As, int B0, int ... Bs>
struct RS_helper<MetaSequenceOfIntegers<As...>,
                 MetaSequenceOfIntegers<B0, Bs...>>
   : RS_helper<MetaSequenceOfIntegers<B0, As...>,
               MetaSequenceOfIntegers<Bs...>>
 { };

template <typename T>
struct RS_helper<T, MetaSequenceOfIntegers<>>
 { using type = T; };

template <int ... Is>
struct RevertSequence
   : RS_helper<MetaSequenceOfIntegers<>, MetaSequenceOfIntegers<Is...>>
 { };

template <int ... Is>
constexpr typename RevertSequence<Is...>::type
   revertSequenceFunction (MetaSequenceOfIntegers<Is...> const &)
 { return {}; }

int main ()
 {
   intergers_sequencer_generator(
      GeneratorOfIntegerSequence<0, int(char, int, char)>::type());

   intergers_sequencer_generator(
      revertSequenceFunction(
         GeneratorOfIntegerSequence<0, int(char, int, char)>::type()));
 }

以上是关于如何反转整数参数包?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 BackStack 上反转片段动画?

编译器显示错误消息“从不强制转换的整数中生成指针”(反转数组)。我该如何解决这个问题? [关闭]

使用 C++ 反转句子中的每个单词需要对我的代码片段进行代码优化

2022&2023华为OD机试 - 单词反转 2(Python)

华为OD机试 - 单词反转(Python)| 真题含思路

如何反转字符串数组并使用指针反转每个字符串?