【发布时间】:2019-06-23 23:13:35
【问题描述】:
考虑这段代码:
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 的类型具有不同的值。例如,如果 T 可以取 2 个值之一(Type1、Type2),我希望 the_length 如果 T==Type1 具有值 10,如果 T==Type2 具有值 20。这可以在 C++11 中完成吗?谢谢
【问题讨论】:
-
“如果
T可以取两个值”是什么意思? -
你的意思是
template<typename T>吗? -
包含一个您想象的呼叫站点外观的示例可能会有所帮助
-
看起来
T有时用作EnumType类型的枚举类型(在参数列表中),有时用作类型名(在std::array的参数列表中)。你能澄清一下吗? -
@astrophobia 你不能使用
T作为std::array的第一个模板参数。T是一个枚举器,但该参数必须是类型名称
标签: c++ c++11 templates template-meta-programming