1

私は任意の数の引数をとり、これらの値に対してブールANDを見つけるテンプレートを作成しています。ペアブールとC++テンプレート

template <bool... Vs> struct meta_bool_and; 

template <bool V> struct meta_bool_and : std::integral_constant<bool, V> {}; 

template <bool V, bool... Vs> 
struct meta_bool_and : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 

しかし、私は次のメッセージ私はこの問題を解決するにはどうすればよい

error: redeclared with 2 template parameters 
struct meta_bool_and : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 

でコンパイルすることができませんでしたか?

答えて

4

部分的な特殊化ではなく、再定義を記述しました。専門化を行うには、専門化するプロパティを指定する必要があります。

これは動作します:

#include <type_traits> 

template <bool... Vs> struct meta_bool_and; 

template <bool V> struct meta_bool_and<V> : std::integral_constant<bool, V> {}; 
//         ^^^ 

template <bool V, bool... Vs> 
struct meta_bool_and<V, Vs...> : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 
//     ^^^^^^^^^^ 

を改良として、あなたは空の組み合わせ(通常は真のように定義)をサポートするかどうかと思います。その場合は、meta_bool_and<bool>ではなくmeta_bool_and<>std::true_typeから派生したもの)に特化しないでください。

1

これらは特殊化されているため、そのように宣言する必要があります。また、あなたはそれを書くことが、代替案として、一つの基地ケース

template <bool V, bool... Vs> 
struct meta_bool_and : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 
// made base case 

template <bool V> 
struct meta_bool_and<V> : std::integral_constant<bool, V> {}; 
// specialization ^^^ 
5

作ることができます。

template <bool ... Bs> 
using meta_bool_and = std::is_same<std::integral_sequence<bool, true, Bs...>, 
            std::integral_sequence<bool, Bs..., true>>; 

またはC++ 1Zで

template <bool ... Bs> 
using meta_bool_and = std::integral_constant<bool, (Bs && ...)>; 
+0

最初のものは、非常にスマートです。私にとってはあまりにもスマートな... – 5gon12eder

関連する問題