このコードを検討してください:
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で実行できますか?ありがとう
古き良き三項演算子の何が問題になっていますか?
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]
コメントを追加