mercredi 19 mai 2021

Check if T is a bitfield?

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][1] 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. 


  [1]: http://github.com/ElDesalmado/pod_reflection

Aucun commentaire:

Enregistrer un commentaire