<legend id='cneTj'><style id='cneTj'><dir id='cneTj'><q id='cneTj'></q></dir></style></legend>
  • <tfoot id='cneTj'></tfoot>

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

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

        尝试将字符串文字作为模板参数传递

        时间:2023-05-24
        <legend id='ANurj'><style id='ANurj'><dir id='ANurj'><q id='ANurj'></q></dir></style></legend>

          <tbody id='ANurj'></tbody>

        <tfoot id='ANurj'></tfoot>

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

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

                • <bdo id='ANurj'></bdo><ul id='ANurj'></ul>
                  本文介绍了尝试将字符串文字作为模板参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我试图找到一种舒适的方式来将字符串文字作为模板参数传递.我并不关心支持尽可能多的编译器,我正在使用带有 --std=c++0x 的最新版本的 g++.

                  I'm trying to find a comfortable way to pass string literals as template arguments. I'm not caring about supporting the widest possible number of compilers, I'm using the latest version of g++ with --std=c++0x.

                  我尝试了很多可能的解决方案,但都让我失望.我有点放弃了,但首先我想知道为什么其中一些失败了.

                  I've tried a lot of possible solutions but all have disappointed me. I'm sort of giving up, but first I'd like to know why a couple of them failed.

                  他们在这里:

                  #include <iostream>
                  #include <string>
                  
                  using namespace std;
                  
                  struct String {
                      char const *m_sz;
                  
                      constexpr String(char const *a_sz)
                          :
                      m_sz(a_sz) {}
                  
                      char const *operator () () const {
                          return m_sz;
                      }
                  };
                  
                  template<class _rstr>
                  string const Get() {
                      return _rstr();
                  }
                  
                  int main() {
                      cout << Get<String("hello")>() << endl;
                      return 0;
                  }
                  

                  还有:

                  #include <iostream>
                  #include <string>
                  
                  using namespace std;
                  
                  struct String {
                      char const *m_sz;
                  
                      constexpr String(char const *a_sz)
                          :
                      m_sz(a_sz) {}
                  };
                  
                  template<String const &_rstr>
                  string const Get() {
                      return _rstr.m_sz;
                  }
                  
                  int main() {
                      String constexpr str = "hello";
                      cout << Get<str>() << endl;
                      return 0;
                  }
                  

                  目标是找到一种舒适的方式将字符串文字传递给无用的 Get 函数,该函数将其模板参数作为 std::string 对象返回.

                  The goal was to find a comfortable way to pass a string literal to the useless Get function, which returns its template argument as an std::string object.

                  抱歉,也许我的主要问题不清楚.我的问题是:为什么这两个片段会失败?

                  sorry, maybe my main question isn't clear. My question is: why do those two snippets fail?

                  推荐答案

                  re:你的 OP:我想知道为什么其中一些失败了.

                  @NatanReed 的评论是正确的:

                  The comment by @NatanReed is correct:

                  • 您的第一个代码段失败了,因为 Get 需要一个 TYPE 并被赋予一个 object.
                  • 您的第二个代码段失败,因为将模板参数定义为对对象的引用是非法的.
                    • 直到 C++2003,即.然后对对象的引用变得合法.
                    • Your first snippet fails because Get needs a TYPE and is given an object.
                    • Your second snippet fails because it is illegal to define a template argument as reference to an object.
                      • until C++2003, that is. Then reference to an object became legal.

                      模板参数必须是一组有限类型的常量.

                      Template arguments must be constants from a limited set of types.

                      • 请参阅:ISO/IEC 14882-2003 §14.1:模板参数
                      • 请参阅:ISO/IEC 14882-2003 §14.3.2:模板非类型参数

                      即便如此,String constexpr str = "hello"; 必须有外部链接.所以把它放在 main() 里面的堆栈是行不通的.

                      And even then, the String constexpr str = "hello"; must have external linkage. So putting it on the stack inside of main() is not going to work.

                      试试这个:

                      #include <iostream>
                      #include <string>
                      
                      using namespace std;
                      
                      struct String {
                          char const *m_sz;
                      
                          constexpr String(char const *a_sz)
                              :
                          m_sz(a_sz) {}
                      };
                      
                      template<String const &_rstr>
                      string const Get() {
                          return _rstr.m_sz;
                      }
                      
                      extern String constexpr globally_visible_str = "hello";
                      int main() {
                          cout << Get<globally_visible_str>() << endl;
                          return 0;
                      }
                      

                      这篇关于尝试将字符串文字作为模板参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:计算以字节为单位设置的位数 下一篇:如何让 std::make_unique 成为我班的朋友

                  相关文章

                  最新文章

                    <tfoot id='81lZN'></tfoot>
                    • <bdo id='81lZN'></bdo><ul id='81lZN'></ul>
                  1. <small id='81lZN'></small><noframes id='81lZN'>

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