使用变量参数的 C++ 模板专业化

问题描述 投票:0回答:1

我正在尝试将 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

c++ templates
1个回答
0
投票

关于:

template <size_t N> Type typeId<char (&)[N]> { return Type_String; }
© www.soinside.com 2019 - 2024. All rights reserved.