std::is_scalar<U...>::value
is_scalar
問題は、単一の型引数のみを取るという事実にあります。複数のブール値を組み合わせたラッパーを作成する必要があります。また、とにかくスカラー型のみが必要な場合に、完全な転送を使用する理由も疑問に思います。値で渡すだけです。U
このように、左辺値が渡されたときに参照として推測されることを心配する必要もありません。
#include <type_traits>
template<bool B>
using bool_ = std::integral_constant<bool, B>;
template<class Head, class... Tail>
struct all_of
: bool_<Head::value && all_of<Tail...>::value>{};
template<class Head>
struct all_of<Head> : bool_<Head::value>{};
template<class C, class T = void>
using EnableIf = typename std::enable_if<C::value, T>::type;
// constructor
template<typename... U>
vector(U... args, EnableIf<all_of<std::is_scalar<U>...>>::type* = 0)
{
unpack_tuple(std::tie(args...)); // tie makes a tuple of references
}
上記のコードは機能するはずです。ただし、アドバイスとして、何かが必要ないstatic_assert
場合は、それを取得せず、そのためにSFINAEを悪用しないようにしてください。:) SFINAEは、オーバーロードされたコンテキストでのみ使用する必要があります。
// constructor
template<typename... U>
vector(U... args)
{
static_assert(all_of<std::is_scalar<U>...>::value, "vector only accepts scalar types");
unpack_tuple(std::tie(args...)); // tie makes a tuple of references
}
実際の質問についてはこれだけですが、インデックスのトリックを使用して、タプル(または一般的な可変個引数、あるいは配列)を解凍するためのより良い方法をお勧めします:
template<unsigned...> struct indices{};
template<unsigned N, unsigned... Is> struct indices_gen : indices_gen<N-1, N-1, Is...>{};
template<unsigned... Is> struct indices_gen<0, Is...> : indices<Is...>{};
template<unsigned... Is, class... U>
void unpack_args(indices<Is...>, U... args){
[](...){}((store[Is] = args, 0)...);
}
template<class... U>
vector(U... args){
static_assert(all_of<std::is_scalar<U>...>::value, "vector only accepts scalar types");
unpack_args(indices_gen<sizeof...(U)>(), args...);
}
このコードが行うことは、可変個引数の解凍メカニズムを「悪用」することです。まず、インデックスのパックを生成し、[0 .. sizeof...(U)-1]
次にこのリストを。と一緒にロックステップで展開しargs
ます。パック拡張は特定の場所でのみ発生する可能性があるため、この拡張を可変個引数(非テンプレート)関数の引数リスト内に配置します。これはその1つです。別の可能性は、ローカル配列としてです。
template<unsigned... Is, class... U>
void unpack_args(indices<Is...>, U... args){
int a[] = {(store[Is] = args, 0)...};
(void)a; // suppress unused variable warnings
}