0

I'm struggling to check at compile time if type is std::bitset or not.

I would like to to something like:

is_bitset<std::bitset<2>>::value; // should evaluate to true
is_bitset<int>::value; // should evaluate to false

I think this SO post is pointing in the right direction, but for some reason I can't manage to make it workd with std::bitset.

What's the best way to do this with C++14?

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
Victor
  • 486
  • 3
  • 8

1 Answers1

5

The solution is very similar to the answer in the link you posted:

template<typename T>
struct is_bitset : std::false_type {};

template<std::size_t N>
struct is_bitset<std::bitset<N>> : std::true_type {};

static_assert(is_bitset<std::bitset<2>>::value); // should evaluate to true
static_assert(!is_bitset<int>::value); // should evaluate to false

Note that the primary template takes a type, while the specialization for bitset takes a non-type parameter.

Here's a demo.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • By the way, you edited your question for a c++14 solution. I believe this will work all the way back to c++11 actually. – cigien May 05 '20 at 14:04