我正在尝试在 C++14 项目中传递文字字符串作为模板参数。谷歌告诉我,我可以这样做:
struct Test {
static const char teststr[];
template<const char* str>
void p() {std::cout << str;}
};
const char Test::teststr[] = "Hello world!";
int main() {
Test t;
t.p<Test::teststr>();
}
Run Code Online (Sandbox Code Playgroud)
它确实有效。
但是,如果我使用const char*, 而不是const char []. 这是行不通的。
struct Test {
static const char* teststr;
template<const char* str>
void p() {std::cout << str;}
};
const char* Test::teststr = "Hello world!";
int main() {
Test t;
t.p<Test::teststr>();
}
Run Code Online (Sandbox Code Playgroud)
现在不行了。编译器告诉我'Test::teststr' is not a valid template argument …
我无法确定这些代码是否应该编译,或者我只是尝试了两个编译器都有错误(GCC 4.2和Sun Studio 12).通常,如果您在头文件中声明了静态类成员,则需要在某个源文件中定义它.但是,静态常量积分的标准中存在例外.例如,允许这样做:
#include <iostream>
struct A {
static const int x = 42;
};
Run Code Online (Sandbox Code Playgroud)
无需在类体外部添加x的定义.我正在尝试做同样的事情,但我也取x的地址并将其传递给模板.这导致链接器错误抱怨缺乏定义.下面的例子没有链接(缺少A :: x的定义),即使它们都在同一个源文件中:
#include <iostream>
template<const int* y>
struct B {
static void foo() { std::cout << "y: " << y << std::endl; }
};
struct A {
static const int x = 42;
typedef B<&x> fooness;
};
int main()
{
std::cout << A::x << std::endl;
A::fooness::foo();
}
Run Code Online (Sandbox Code Playgroud)
这是奇怪的,因为只要我不将地址传递给模板,它就可以工作.这是一个错误还是某种技术上符合标准的?
编辑:我应该指出&A :: x 不是运行时值.在编译时为逻辑分配的变量留出内存.