Я хочу иметь возможность специализироваться на основе постоянной строки стиля c. Проблема в том, что когда я вызываю мою шаблонную функцию, типом является const char [N], где «N» - это размер строки +1 (нулевой символ). Как я могу специализироваться для всех строк в стиле c?
Следующий код отображает проблему. Вы можете видеть, что специализация для const char [15] совпадает с "const char [15]", но для "const char [5]" она переходит к Generic.
Есть ли способ сделать это?
template <typename T>
struct Test {
static const char* type() { return "Generic"; }
};
template <>
struct Test<const char*> {
static const char* type() { return "const char*"; }
};
template <>
struct Test<const char[]> {
static const char* type() { return "const char[]"; }
};
template <>
struct Test<const char[15]> {
static const char* type() { return "const char[15]"; }
};
template <>
struct Test<char*> {
static const char* type() { return "char*"; }
};
template <>
struct Test<char[]> {
static const char* type() { return "char[]"; }
};
template <typename T>
void PrintType(const T& expected) {
std::cerr << expected << " type " << Test<T>::type() << std::endl;
}
int main(int argc, char* argv[]) {
const char* tmp = "const char*";
PrintType(tmp);
PrintType("const char[]");
PrintType("const char[15]");
PrintType("const char[5]");
}
вывод при запуске в Windows 7 - VS 2008
const char* type const char*
const char[] type Generic
const char[15] type const char[15]
const char[5] type Generic