相关疑难解决方法(0)

传递const char*作为模板参数

为什么你不能在这里传递文字字符串?我使用了一个非常轻微的解决方法.

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();
}
Run Code Online (Sandbox Code Playgroud)

c++ string templates

28
推荐指数
4
解决办法
3万
查看次数

标签 统计

c++ ×1

string ×1

templates ×1