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

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

问题描述

我试图找到一种舒适的方式来将字符串文字作为模板参数传递.我并不关心支持尽可能多的编译器,我正在使用带有 --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;
      }
      

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

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