2

I'm using BOOST_FUSION_ADAPT_STRUCT(), and I need to check that all the members are declared and in the correct order. So first I did this:

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?");
    }
};

And this works:

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

Next I want to make sure the order is correct, so for example (x, z, y) in the above example should fail to compile. But so far I've only figured out a runtime solution (added to check()):

        const Sequence* dummy = nullptr;
        ++dummy;
        boost::fusion::for_each(*dummy, struct_offset_checker());

Using this functor:

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;
    }
};

But I'd rather have a compile-time solution. Can you think of one?

The funny thing is that GCC is actually able to figure out at compile time when an exception will be thrown, if I have -Wsuggest-attribute=noreturn - it tells me when a function which calls check() would not return (due to the logic_error).

If you want to try it yourself, the relevant headers are:

#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>
John Zwinck
  • 239,568
  • 38
  • 324
  • 436

2 Answers2

2

In order to perform a compile time check you can iterate yourself in a constexpr way over the adapted sequence using std::index_sequence:

#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();
}

as desired, this fails with:

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

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • 1
    Very nice answer, I think [this](http://coliru.stacked-crooked.com/a/808f1d16fe3a29c6) could also work. – llonesmiz Sep 29 '16 at 07:58
  • @jv_: Thank you and also m.s. I drew upon your solutions to make my own, which I posted as an answer here. A bit shorter, but generally it does what the two of you did. – John Zwinck Sep 30 '16 at 06:50
1

Using m.s.'s answer here as inspiration, I drew up a solution using Boost.MPL instead of std::make_index_sequence<>, partly because I'm more accustomed to this style:

typedef mpl::range_c<unsigned, 0, fusion::result_of::size<Sequence>::value - 1> IndicesWithoutLast;
mpl::for_each<IndicesWithoutLast>(struct_offset_checker<Sequence>());

Using this functor:

template <typename Sequence>
struct struct_offset_checker
{
    template <typename Index>
    constexpr void operator()(Index)
    {
        typedef typename mpl::plus<Index, mpl::int_<1>>::type NextIndex;

        constexpr Sequence* dummy = nullptr;
        constexpr void* curr = &fusion::at<Index>(*dummy);
        constexpr void* next = &fusion::at<NextIndex>(*dummy);
        static_assert(curr < next, "fields are out of order");
    }
};
John Zwinck
  • 239,568
  • 38
  • 324
  • 436