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

  • <legend id='U5J9t'><style id='U5J9t'><dir id='U5J9t'><q id='U5J9t'></q></dir></style></legend>

      <bdo id='U5J9t'></bdo><ul id='U5J9t'></ul>

    <tfoot id='U5J9t'></tfoot>
      1. C++在编译时将整数转换为字符串

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

            • <bdo id='hjf7T'></bdo><ul id='hjf7T'></ul>
              • <legend id='hjf7T'><style id='hjf7T'><dir id='hjf7T'><q id='hjf7T'></q></dir></style></legend>

                  <small id='hjf7T'></small><noframes id='hjf7T'>

                  <tfoot id='hjf7T'></tfoot>
                  本文介绍了C++在编译时将整数转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想做这样的事情:

                  template<int N>
                  char* foo() {
                    // return a compile-time string containing N, equivalent to doing
                    // ostringstream ostr; 
                    // ostr << N;
                    // return ostr.str().c_str();
                  }
                  

                  似乎 boost MPL 库可能允许这样做,但我无法真正弄清楚如何使用它来完成此操作.这可能吗?

                  It seems like the boost MPL library might allow this but I couldn't really figure out how to use it to accomplish this. Is this possible?

                  推荐答案

                  首先,如果您通常在运行时知道数字,您就可以轻松构建相同的字符串.也就是说,如果你的程序中有 12,你也可以有 "12".

                  First of all, if usually you know the number at run time, you can as easily build the same string. That is, if you have 12 in your program, you can have also "12".

                  预处理器宏还可以为参数添加引号,因此您可以编写:

                  Preprocessor macros can add also quotes to arguments, so you can write:

                  #define STRINGIFICATOR(X) #X
                  

                  这个,每当你写STRINGIFICATOR(2)时,它都会产生2".

                  This, whenever you write STRINGIFICATOR(2), it will produce "2".

                  然而,它实际上可以在没有宏的情况下完成(使用编译时元编程).这并不简单,所以我不能给出确切的代码,但我可以给你一些关于如何去做的想法:

                  However, it actually can be done without macros (using compile-time metaprogramming). It is not straightforward, so I cannot give the exact code, but I can give you ideas on how to do it:

                  1. 使用要转换的数字编写递归模板.模板将递归到基本情况,即数量小于 10.
                  2. 在每次迭代中,您可以将 N%10 位数字转换为 T.E.D.建议,使用 mpl::string 来构建附加该字符的编译时字符串.
                  3. 您最终将构建一个 mpl::string,它具有一个静态的 value() 字符串.
                  1. Write a recursive template using the number to be converted. The template will recurse till the base case, that is, the number is less than 10.
                  2. In each iteration, you can have the N%10 digit to be converted into a character as T.E.D. suggests, and using mpl::string to build the compile-time string that appends that character.
                  3. You'll end up building a mpl::string, that has a static value() string.

                  我花时间将其作为个人练习来实施.最后还不错:

                  I took the time to implement it as a personal exercise. Not bad at the end:

                  #include <iostream>
                  #include <boost/mpl/string.hpp>
                  
                  using namespace boost;
                  
                  // Recursive case
                  template <bool b, unsigned N>
                  struct int_to_string2
                  {
                          typedef typename mpl::push_back<
                                  typename int_to_string2< N < 10, N/10>::type
                                                           , mpl::char_<'0' + N%10>
                                                           >::type type;
                  };
                  
                  // Base case
                  template <>
                  struct int_to_string2<true,0>
                  {
                          typedef mpl::string<> type;
                  };
                  
                  
                  template <unsigned N>
                  struct int_to_string
                  {
                          typedef typename mpl::c_str<typename int_to_string2< N < 10 , N>::type>::type type;
                  };
                  
                  int
                  main (void)
                  {
                          std::cout << int_to_string<1099>::type::value << std::endl;
                          return 0;
                  }
                  

                  这篇关于C++在编译时将整数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 C++ 模板实现访问者模式 下一篇:为什么模板参数中的 enable_if_t 会抱怨重新定义

                  相关文章

                  最新文章

                  <small id='2Gh75'></small><noframes id='2Gh75'>

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

                        <bdo id='2Gh75'></bdo><ul id='2Gh75'></ul>
                    1. <legend id='2Gh75'><style id='2Gh75'><dir id='2Gh75'><q id='2Gh75'></q></dir></style></legend>

                    2. <tfoot id='2Gh75'></tfoot>