我正在尝试将 c 风格变体包装在库中。它使用类型枚举,我的意思是使用模板来填充。
简化版本大致如下:
enum Type{
Type_Int,
Type_Double,
Type_String,
};
template<typename T>
Type typeId();
template<> Type typeId<int>() { return Type_Int; }
template<> Type typeId<double>(){ return Type_Double; }
template<> Type typeId<const char*>() { return Type_String; }
template<> Type typeId<char[10]>() { return Type_String; }
// not allowed
//template<int N> Type typeId<char[N]>() { return Type_String; }
};
https://godbolt.org/z/3GsKv6PEn
我可以很好地识别
char[10]
,但我想对char[N]
做一个一般情况,这是不允许的。那么我该如何识别所有 char[N]
类型呢?
ps。它是嵌入式的,所以我不喜欢尽可能使用
std::string
。
关于:
template <size_t N> Type typeId<char (&)[N]> { return Type_String; }