尝试传递字符串文字作为模板参数 [英] Trying to pass string literals as template arguments

查看:303
本文介绍了尝试传递字符串文字作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



这里他们是:

  #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 b $ b 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对象。



编辑:对不起,也许我的主要问题不清楚。我的问题是:为什么这两个片断都会失败?

解决方案

re:你的OP:



@NatanReed的注释是正确的:




  • 您的第一个代码段失败,因为获取需要 TYPE 对象

  • 您的第二个代码段失败,因为将模板参数定义为对象的引用是非法的。


    • ,直到C ++ 2003,即。 c> b $ b

      模板参数必须是有限类型的常量。




      • 参见:ISO / IEC 14882-2003§14.1 :模板参数

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



      即使这样, String constexpr str =hello; 必须有外部链接。所以把它放在 main()里面的堆栈是不行的。



        #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 global_visible_str =hello;
      int main(){
      cout<< Get< global_visible_str>()<< endl;
      return 0;
      }


      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.

      Here they are:

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

      And:

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

      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.

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

      解决方案

      re: your OP: I'd like to know why a couple of them failed.

      The comment by @NatanReed is correct:

      • 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.

      • See: ISO/IEC 14882-2003 §14.1: Template parameters
      • See: ISO/IEC 14882-2003 §14.3.2: Template non-type arguments

      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.

      Give this a try:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆