【发布时间】:2011-04-15 23:03:56
【问题描述】:
为什么你不能在这里传递文字字符串?我用一个非常轻微的解决方法让它工作。
template<const char* ptr> struct lols {
lols() : i(ptr) {}
std::string i;
};
class file {
public:
static const char arg[];
};
decltype(file::arg) file::arg = __FILE__;
// Getting the right type declaration for this was irritating, so I C++0xed it.
int main() {
// lols<__FILE__> hi;
// Error: A template argument may not reference a non-external entity
lols<file::arg> hi; // Perfectly legal
std::cout << hi.i;
std::cin.ignore();
std::cin.get();
}
【问题讨论】:
-
const char file::arg[] = __FILE__;这应该可以让你回到 C++03 :)。 -
@Kenny:我试过了。编译器抛出错误。
-
@Dead:奇怪。 g++ 编译良好(使用 -pedantic)。
-
@Kenny:烦人,我一直认为这是不可能的。现在我有了它,我似乎无法让它做任何有趣或有用的事情。
-
请注意,如果在命名空间范围内,您将需要
extern const char arg[] = __FILE__;为数组提供外部链接(默认情况下,const 对象具有内部链接)。