'char const * str'作为模板参数

时间:2014-01-18 16:23:57

标签: c++ templates const

假设我有一个模板:

template<char const *str>
class Template { ... };

为什么不能写下面的内容?

Template<"literal"> T;

char const *s = "Literal";
Template<s> T;

为什么以下有效?

char const s[] = "Literal";
Template<s> T;

1 个答案:

答案 0 :(得分:0)

因为模板类基于类型而不是基于值 模板旨在帮助您编写更通用的代码,这对于几种类型的数据是相同的,因此您不必再为每种类型编写代码。

相关问题