问题描述
我试图找到一种舒适的方式来将字符串文字作为模板参数传递.我并不关心支持尽可能多的编译器,我正在使用带有 --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 aTYPE
and is given anobject
. - 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 ofmain()
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; }
这篇关于尝试将字符串文字作为模板参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除! - until C++2003, that is. Then
- 直到 C++2003,即.然后