• <tfoot id='HGDhf'></tfoot>

    <legend id='HGDhf'><style id='HGDhf'><dir id='HGDhf'><q id='HGDhf'></q></dir></style></legend>

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

        提取 C++ 模板参数

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

              <bdo id='xUmIU'></bdo><ul id='xUmIU'></ul>
              <tfoot id='xUmIU'></tfoot>

                1. <small id='xUmIU'></small><noframes id='xUmIU'>

                2. 本文介绍了提取 C++ 模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  虽然我很怀疑,但我很好奇是否可以从现有类型中提取原始类型模板参数,也许使用 RTTI.

                  Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template parameters from an existing type, perhaps using RTTI.

                  例如:

                  typedef std::bitset<16> WordSet;
                  

                  是否可以在不将其硬编码到其他地方的情况下提取上述代码中的数字 16?欢迎使用特定于编译器的实现,但我对 g++ 特别感兴趣.

                  Would it be possible to extract the number 16 in the above code without hard-coding it elsewhere? Compiler specific implementations are welcome, though I'm particularly interested in g++.

                  推荐答案

                  通常不可能选择任意模板参数.

                  It's not possible in general to pick arbitrary template parameters.

                  但是,通常的做法是这样的:

                  However, the usual way you do it is this:

                  template<int N>
                  struct foo {
                      static const int value = N;
                  };
                  

                  和类型

                  template<typename T>
                  struct foo {
                      typedef T type;
                  };
                  

                  然后您可以通过 foo<39>::valuefoo::type 访问它.

                  You can access it then as foo<39>::value or foo<int>::type.

                  如果你有一个特定的类型,你可以使用部分模板特化:

                  If you have a particular type, you can use partial template specialization:

                  template<typename>
                  struct steal_it;
                  
                  template<std::size_t N>
                  struct steal_it< std::bitset<N> > {
                      static const std::size_t value = N;
                  };
                  

                  事实上,同样的原则也适用于类型参数.现在您可以将任何位集传递给它,例如 steal_it<std::bitset<16>>::value(注意使用 size_t,而不是 int!).因为我们还没有可变参数 many 模板参数,我们必须将自己限制在一个特定的参数计数,并为从 1 到 N 的计数重复steal_it 模板特化.另一个困难是扫描具有混合参数的类型(类型和非-类型参数).这可能很难解决.

                  The same principle is possible for type parameters too, indeed. Now you can pass any bitset to it, like steal_it< std::bitset<16> >::value (note to use size_t, not int!). Because we have no variadic many template paramters yet, we have to limit ourself to a particular parameter count, and repeat the steal_it template specializations for count from 1 up to N. Another difficulty is to scan types that have mixed parameters (types and non-types parameters). This is probably nontrivial to solve.

                  如果你没有类型,而只有它的一个对象,你可以使用一个技巧,在编译时仍然获得一个值:

                  If you have not the type, but only an object of it, you can use a trick, to still get a value at compile time:

                  template<typename T>
                  char (& getN(T const &) )[steal_it<T>::value];  
                  
                  int main() {
                      std::bitset<16> b;
                      sizeof getN(b); // assuming you don't know the type, you can use the object
                  }
                  

                  诀窍是让函数模板自动推断类型,然后返回对字符数组的引用.函数不需要定义,唯一需要的是它的类型.

                  The trick is to make the function template auto-deduce the type, and then return a reference to a character array. The function doesn't need to be defined, the only thing needed is its type.

                  这篇关于提取 C++ 模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 模板和内联 下一篇:C++ 协变模板

                  相关文章

                  最新文章

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

                    <legend id='OdHWx'><style id='OdHWx'><dir id='OdHWx'><q id='OdHWx'></q></dir></style></legend>

                      <bdo id='OdHWx'></bdo><ul id='OdHWx'></ul>
                  1. <small id='OdHWx'></small><noframes id='OdHWx'>