gpt4 book ai didi

c++ - 增强融合 : validate adapted struct member ordering at compile time

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:21 24 4
gpt4 key购买 nike

我正在使用 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;
}
};

但我宁愿有一个编译时解决方案。你能想到一个吗?

有趣的是,如果我有 -Wsuggest-attribute=noreturn,GCC 实际上能够在编译时确定何时抛出异常 - 它告诉我何时调用的函数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>

最佳答案

为了执行编译时检查,您可以使用 std::index_sequenceconstexpr 方式自行迭代适应序列:

#include <boost/fusion/adapted.hpp>
#include <boost/fusion/include/at.hpp>

struct foo
{
char c;
int x;
float y;
double z;
};


BOOST_FUSION_ADAPT_STRUCT(foo, x, c, y, z)

template <typename Sequence>
struct check_order
{
template <std::size_t First, std::size_t Second>
static constexpr bool internal()
{
constexpr Sequence* s = nullptr;
const void* first = &boost::fusion::at_c<First>(*s);
const void* second = &boost::fusion::at_c<Second>(*s);
if (second <= first)
{
throw std::logic_error("struct member is declared in a different order");
}
return true;
}

template <std::size_t... Is>
static constexpr bool run(std::index_sequence<Is...>)
{
int list[] = {(internal<Is,Is+1>(),0)...};
(void)list;
return true;
}

static constexpr void check()
{
constexpr std::size_t size = boost::fusion::result_of::size<Sequence>::type::value;
static_assert(run(std::make_index_sequence<size-1>{}), "");
}
};


int main()
{
check_order<foo>::check();
}

根据需要,这失败了:

main.cpp: In instantiation of 'static constexpr void check_order<Sequence>::check() [with Sequence = foo]':
main.cpp:49:23: required from here
main.cpp:42:9: error: non-constant condition for static assertion
static_assert(run(std::make_index_sequence<size-1>{}), "");
^~~~~~~~~~~~~
main.cpp:42:26: in constexpr expansion of 'check_order<Sequence>::run<{0ul, 1ul, 2ul}>((std::make_index_sequence<3ul>{}, std::make_index_sequence<3ul>()))'
main.cpp:34:41: in constexpr expansion of 'check_order<Sequence>::internal<0ul, 1ul>()'

live example

关于c++ - 增强融合 : validate adapted struct member ordering at compile time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com