相关疑难解决方法(0)

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

我正在尝试找到一种将字符串文字作为模板参数传递的舒适方法.我并不关心支持尽可能多的编译器,我正在使用最新版本的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 () () const {
        return m_sz;
    }
};

template<class _rstr>
string const Get() {
    return _rstr();
}

int main() {
    cout << Get<String("hello")>() << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

和:

#include <iostream>
#include <string>

using namespace std;

struct String {
    char const *m_sz;

    constexpr String(char const *a_sz)
        :
    m_sz(a_sz) {} …
Run Code Online (Sandbox Code Playgroud)

c++ string templates literals c++11

7
推荐指数
4
解决办法
1万
查看次数

标签 统计

c++ ×1

c++11 ×1

literals ×1

string ×1

templates ×1