检查 T 是否是位域?

问题描述

是否可以在编译时检查 T 是否为位域?

我想将其用于:

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

示例用法

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

我正在实现一个 reflection library,它为类似 pod 的结构提供 for_eachget<Index> 类似元组函数。不幸的是,您不能在具有位域的结构上使用它,因为它是 UB获取位域的地址。所以我想检测一个结构是否包含一个位域来触发编译时错误。现在我有了一些解决方法 - 我将结构体大小与评估值进行比较,但我想要一种更简单的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)