为什么你不能在这里传递文字字符串?我做了一个非常轻微的解决方法。
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();
}
原文由 Puppy 发布,翻译遵循 CC BY-SA 4.0 许可协议
因为这不是一个有用的实用程序。由于它们不是模板参数的允许形式,因此它目前不起作用。
让我们假设它们有效。因为它们不需要为使用的相同值具有相同的地址,所以即使您的代码中具有相同的字符串文字值,您也会得到不同的实例化。
您可以为您的文本编辑器编写一个插件,用逗号分隔的字符文字列表替换字符串并返回。使用可变参数模板,您可以以某种方式“解决”这个问题。