我试图找到一种舒适的方式来将字符串文字作为模板参数传递.我并不关心支持尽可能多的编译器,我正在使用带有 --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
.对对象的引用
变得合法.Get
needs a TYPE
and is given an object
.reference to an object
became legal.模板参数必须是一组有限类型的常量.
Template arguments must be constants from a limited set of types.
即便如此,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;
}
这篇关于尝试将字符串文字作为模板参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!