我在用着BOOST_FUSION_ADAPT_STRUCT()
,我需要检查所有成员是否已声明且顺序正确。所以首先我这样做了:
template <typename Sequence>
struct checker
{
static void check()
{
typedef typename mpl::accumulate<Sequence, mpl::size_t<0>,
mpl::plus<mpl::_1, mpl::sizeof_<mpl::_2>>>::type total_size;
static_assert(sizeof(Sequence) == total_size::value, "omitted field?");
}
};
这有效:
struct foo
{
int x;
float y;
double z;
};
BOOST_FUSION_ADAPT_STRUCT(foo, x, y, z);
checker<foo>::check(); // fails if any field is missing
接下来我想确保顺序是正确的,例如(x, z, y)
上面的例子应该无法编译。但到目前为止我只找到了一个运行时解决方案(添加到check()
):
const Sequence* dummy = nullptr;
++dummy;
boost::fusion::for_each(*dummy, struct_offset_checker());
使用这个函子:
struct struct_offset_checker
{
mutable const void* _last = nullptr;
template <typename Element>
void operator()(const Element& element) const
{
if (&element <= _last)
throw std::logic_error("struct member is declared in a different order");
_last = &element;
}
};
但我宁愿有一个编译时解决方案。你能想到一个吗?
有趣的是,GCC 实际上能够在编译时计算出何时抛出异常,如果我有的话-Wsuggest-attribute=noreturn
- 它告诉我函数何时调用check()
不会回来(由于logic_error
).
如果你想自己尝试一下,相关的标题是:
#include <stdexcept>
#include <boost/fusion/adapted.hpp>
#include <boost/mpl/accumulate.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/sizeof.hpp>
#include <boost/mpl/size_t.hpp>