1

I am writing a template that takes a arbitrary number of arguments and find the Boolean AND on these value.

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> {}; 

However, I failed to compile by the following message

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

How can I fix this problem?

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
Jes
  • 2,614
  • 4
  • 25
  • 45

3 Answers3

8

As alternatives, you may write it:

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

or in c++17:

template <bool ... Bs>
using meta_bool_and = std::integral_constant<bool, (Bs && ...)>;
Jarod42
  • 203,559
  • 14
  • 181
  • 302
4

You have written a re-definition instead of a partial specialization. In order to provide a specialization, you must specify what properties you specialize on.

This will work:

#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> {}; 
//                  ^^^^^^^^^^

As an improvement, think whether you want to support the empty conjunction (typically defined as true). If so, don't specialize on meta_bool_and<bool> but on meta_bool_and<> (derived from std::true_type).

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
1

Since these are specializations, they need to be declared as such. You can also make one the base case

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   ^^^
kmdreko
  • 42,554
  • 6
  • 57
  • 106