• <bdo id='5aHD6'></bdo><ul id='5aHD6'></ul>
        <legend id='5aHD6'><style id='5aHD6'><dir id='5aHD6'><q id='5aHD6'></q></dir></style></legend>
      1. <tfoot id='5aHD6'></tfoot>

        <small id='5aHD6'></small><noframes id='5aHD6'>

        <i id='5aHD6'><tr id='5aHD6'><dt id='5aHD6'><q id='5aHD6'><span id='5aHD6'><b id='5aHD6'><form id='5aHD6'><ins id='5aHD6'></ins><ul id='5aHD6'></ul><sub id='5aHD6'></sub></form><legend id='5aHD6'></legend><bdo id='5aHD6'><pre id='5aHD6'><center id='5aHD6'></center></pre></bdo></b><th id='5aHD6'></th></span></q></dt></tr></i><div id='5aHD6'><tfoot id='5aHD6'></tfoot><dl id='5aHD6'><fieldset id='5aHD6'></fieldset></dl></div>

        模板元程序将类型转换为唯一编号

        时间:2023-05-24
      2. <i id='4xDyp'><tr id='4xDyp'><dt id='4xDyp'><q id='4xDyp'><span id='4xDyp'><b id='4xDyp'><form id='4xDyp'><ins id='4xDyp'></ins><ul id='4xDyp'></ul><sub id='4xDyp'></sub></form><legend id='4xDyp'></legend><bdo id='4xDyp'><pre id='4xDyp'><center id='4xDyp'></center></pre></bdo></b><th id='4xDyp'></th></span></q></dt></tr></i><div id='4xDyp'><tfoot id='4xDyp'></tfoot><dl id='4xDyp'><fieldset id='4xDyp'></fieldset></dl></div>
          <tfoot id='4xDyp'></tfoot>

              <bdo id='4xDyp'></bdo><ul id='4xDyp'></ul>

                <small id='4xDyp'></small><noframes id='4xDyp'>

                  <tbody id='4xDyp'></tbody>
                • <legend id='4xDyp'><style id='4xDyp'><dir id='4xDyp'><q id='4xDyp'></q></dir></style></legend>
                  本文介绍了模板元程序将类型转换为唯一编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我刚刚开始玩元编程,我正在处理不同的任务,只是为了探索这个领域.其中之一是生成一个唯一的整数并将其映射到类型,如下所示:

                  I just started playing with metaprogramming and I am working on different tasks just to explore the domain. One of these was to generate a unique integer and map it to type, like below:

                  int myInt = TypeInt<AClass>::value;
                  

                  其中 value 应该是编译时常量,这反过来可以在元程序中进一步使用.

                  Where value should be a compile time constant, which in turn may be used further in meta programs.

                  我想知道这是否可能,在这种情况下如何.因为虽然我在探索这个主题方面学到了很多东西,但我仍然没有想出答案.

                  I want to know if this is at all possible, and in that case how. Because although I have learned much about exploring this subject I still have failed to come up with an answer.

                  (P.S. 是/否的答案比不使用元编程的 C++ 解决方案更令人满意,因为这是我正在探索的领域)

                  (P.S. A yes/no answer is much more gratifying than a c++ solution that doesn't use metaprogramming, as this is the domain that I am exploring)

                  推荐答案

                  到目前为止,我最接近的是能够保留一个类型列表,同时跟踪回基地的距离(给出一个唯一值).请注意,如果您正确跟踪事物,此处的位置"对于您的类型将是唯一的(请参阅示例的主要内容)

                  The closest I've come so far is being able to keep a list of types while tracking the distance back to the base (giving a unique value). Note the "position" here will be unique to your type if you track things correctly (see the main for the example)

                  template <class Prev, class This>
                  class TypeList
                  {
                  public:
                     enum
                     {
                        position = (Prev::position) + 1,
                     };
                  };
                  
                  template <>
                  class TypeList<void, void>
                  {
                  public:
                    enum
                    {
                       position = 0,
                    };
                  };
                  
                  
                  #include <iostream>
                  
                  int main()
                  {
                          typedef TypeList< void, void> base;  // base
                          typedef TypeList< base, double> t2;  // position is unique id for double
                          typedef TypeList< t2, char > t3; // position is unique id for char
                  
                          std::cout << "T1 Posn: " << base::position << std::endl;
                          std::cout << "T2 Posn: " << t2::position << std::endl;
                          std::cout << "T3 Posn: " << t3::position << std::endl;
                  
                  }
                  

                  这行得通,但我自然不想以某种方式指定prev"类型.最好找出一种方法来自动跟踪.也许我会再玩一些,看看是否有可能.绝对是一个有趣/有趣的谜题.

                  This works, but naturally I'd like to not have to specify a "prev" type somehow. Preferably figuring out a way to track this automatically. Maybe I'll play with it some more to see if it's possible. Definitely an interesting/fun puzzle.

                  这篇关于模板元程序将类型转换为唯一编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 中类与函数的模板类型推导? 下一篇:C++ 表达式模板

                  相关文章

                  最新文章

                    <legend id='MXJ4b'><style id='MXJ4b'><dir id='MXJ4b'><q id='MXJ4b'></q></dir></style></legend>
                    <tfoot id='MXJ4b'></tfoot>

                    • <bdo id='MXJ4b'></bdo><ul id='MXJ4b'></ul>

                      <i id='MXJ4b'><tr id='MXJ4b'><dt id='MXJ4b'><q id='MXJ4b'><span id='MXJ4b'><b id='MXJ4b'><form id='MXJ4b'><ins id='MXJ4b'></ins><ul id='MXJ4b'></ul><sub id='MXJ4b'></sub></form><legend id='MXJ4b'></legend><bdo id='MXJ4b'><pre id='MXJ4b'><center id='MXJ4b'></center></pre></bdo></b><th id='MXJ4b'></th></span></q></dt></tr></i><div id='MXJ4b'><tfoot id='MXJ4b'></tfoot><dl id='MXJ4b'><fieldset id='MXJ4b'></fieldset></dl></div>
                    1. <small id='MXJ4b'></small><noframes id='MXJ4b'>