考虑以下代码:
enum class EnumType
{
Type1,
Type2
};
constexpr std::size_t the_length;
template <EnumType T>
int function()
{
std::array<uint8_t, the_length> x;
//some code here that uses x
}
我希望数组的长度x
根据类型具有不同的值T
。例如,如果ifT
可以取2个值(Type1
,Type2
)之一,则我希望the_length
值为10 if T==Type1
,如果值为20 T==Type2
。可以在C ++ 11中完成吗?谢谢
好的老三元运算符怎么了?
template <EnumType T>
int function()
{
std::array<SomeType, T == EnumType::Type1 ? 10u : 20u> x;
}
如果T
是typename
,而不是某种类型的值,则只需更改测试
template <typename T>
int function()
{
std::array<T, std::is_same<SomeType1, T>::value ? 10u : 20u> x;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句