【发布时间】:2016-08-21 09:28:46
【问题描述】:
如果我有一个枚举定义为 ...
enum MyValue
{
Unk,
A,
B
};
我想创建一个专门的模板,根据变量类型本身返回/设置类型
template<typename T>
struct get_value
{
// the 'value' should be MyValue::Unk
};
template<>
struct get_value<int>
{
// the 'value' should be MyValue::A
};
template<>
struct get_value<double>
{
// the 'value' should be MyValue::B
};
这样我就可以调用结构了
auto x = get_value<char>::value; // == MyValue::Unk
和
auto y = get_value<int>::value; // == MyValue::A
在c++中是否可以,如果可以,如何实现?
【问题讨论】: