John Zwinck
John Zwinck

Reputation: 249394

Boost Fusion: validate adapted struct member ordering at compile time

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>

Upvotes: 2

Views: 615

Answers (2)

John Zwinck
John Zwinck

Reputation: 249394

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

Upvotes: 1

m.s.
m.s.

Reputation: 16344

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

Upvotes: 2

Related Questions