2 つのテンプレート パラメーターを受け取るテンプレート関数の C++11 でのコンパイル時に、どちらも符号なし整数型である必要があります。ローカル変数に、2 つのテンプレート パラメーターのどちらかビット数が多い方の型を持たせたいと考えています。C++03 では、次のように記述します。
template<bool, class T, class U>
struct pick_first;
template<class T, class U>
struct pick_first<true, T, U> {
typedef T type;
};
template<class T, class U>
struct pick_first<false, T, U> {
typedef U type;
};
template<class T, class U>
struct pick_bigger {
typedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;
};
// usage
template<class uintX_t, class uintY_t>
void foo() {
typename pick_bigger<uintX_t, uintY_t>::type mylocal = 0;
// insert doing stuff with mylocal here
}
これを簡単にするために、C++11 の新しい機能を利用できますか? 可変個引数テンプレートを使用して、型のペアだけでなく、pick_first を使用する代わりに、新しい int_leastX_t および int_fastX_t 型で動作するように多くの特殊化を記述できることを知っています。しかし、これに対する単純なより良いアプローチがあるかどうか、私は興味があります。多分どういうわけかauto/constexpr/decltypeを活用していますか?