Check if T is a bitfield?

Viewed 88

Is it possible to check at compile time if T is a bitfield?

I'd like to use it in:

template <typename Allowed>
struct explicitly_constructible
{
   template <typename T, typename = std::enable_if_t<is_bitfield_v<T>>>
   constexpr operator T() const;
};

Example usage:

struct normal
{
   int foo;
};

struct with_bitfield
{
  int bar : 2;
};

normal n{explicitly_constructible<int>()}; // well-formed
with_bitfield m{explicitly_constructible<int>()}; // must be ill-formed

I am implementing a reflection library that provides for_each and get<Index> tuple-like functions for pod-like structs. Unfortunately, you can't use it on a struct with bitfields since it is UB to get an address of a bit-field. So I want to detect if a struct contains a bitfield to fire a compile-time error. Now I have some workaround - I compare struct size against evaluated, but I would like a simpler approach.

0 Answers
Related