7

I'm currently trying to calculate an offset of a data member in a boost fusion adapted structure, but I am not sure if there is an elegant way to do so. I'd like to do something like the following:

#include <iostream> 
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/include/at.hpp>
#include <cstddef.h>

   struct test {
      int a;  
      char c;  
      double b;  
    };

BOOST_FUSION_ADAPT_STRUCT(
  test, 
  (int, a)
  (char, c)
  (double, b)
)

int main() {
test s{1, 2, 3.0}; 
// The following code doesn't work... I'm just trying to get my point across
std::cout << "offset is :" << offsetof(test, at<1>(s)) << std::endl;
}

The point is that I don't want to have to explicitly state 'a' or 'b' or 'c' in the offsetof function. This by itself isn't that useful, but I'd like to use it in a boost for_each loop so I can compute the offsets of all the data members of any structs at compile time.

If you have any ideas, I'd love to hear them!

sehe
  • 374,641
  • 47
  • 450
  • 633
user985030
  • 1,557
  • 1
  • 16
  • 32
  • 3
    [This](http://coliru.stacked-crooked.com/a/cdb50e9a73546a82) is probably not a good approach, but since you'd love to hear ideas... :P. The `fusion::for_each` uses a c++14 lambda, if you can't use them you'll need to use a templated functor (something like [this](http://stackoverflow.com/a/17842958/2417774)). – llonesmiz Aug 25 '15 at 16:09
  • @cv_and_he wow. Impressed. That's pretty nifty (to me, the PP macros are a barrier; it's nice to see you walk right over those) – sehe Aug 25 '15 at 19:12
  • That is pretty cool! Thanks for sharing the code! – user985030 Aug 25 '15 at 19:48
  • you are missing the boost-fusion tag – Gladaed Apr 06 '18 at 11:43
  • @nullqube clearly it is, someone added a bounty. – sehe Apr 08 '18 at 21:32
  • @llonesmiz could you consider adding an answer with that code? – sehe Apr 08 '18 at 21:32
  • 2
    @sehe Done. I think I have managed to simplify it a little bit. – llonesmiz Apr 09 '18 at 06:17

1 Answers1

5

I'm not an expert, but I think that due to the nature of offsetof there is no way to achieve what you want without the need of using macros. The following example simply uses a macro to create a trait for each member of the struct that returns its precomputed offset whenever you access it.

Running on Wandbox

#include <iostream> 
#include <string>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/mpl.hpp>

#include <boost/mpl/range_c.hpp>

#include <cstddef>

namespace not_fusion
{
    template <typename StructName, int N>
    struct offset_of;
}

//Actually creates each respective 'offset_of' trait 
//you could change 'BOOST_STATIC_CONSTANT(std::size_t,...)'
//with a 'static constexpr std::size_t ...' or whatever you like
#define CREATE_OFFSET_TRAIT(R,STRUCT_NAME,INDEX,MEMBER) \
template <> struct offset_of<STRUCT_NAME, INDEX>{ BOOST_STATIC_CONSTANT(std::size_t, value = offsetof(STRUCT_NAME,MEMBER)); };

//Iterates the struct members in order to create the corresponding 'offset_of' traits
#define NOT_FUSION_SAVE_OFFSETS(STRUCT_NAME,MEMBERS) \
namespace not_fusion { \
BOOST_PP_SEQ_FOR_EACH_I(CREATE_OFFSET_TRAIT,STRUCT_NAME,MEMBERS) \
}

//Simply "invokes" 'BOOST_FUSION_ADAPT_STRUCT' and 'NOT_FUSION_SAVE_OFFSETS'
#define ADAPT_STRUCT_AND_SAVE_OFFSETS(TYPE,...) \
BOOST_FUSION_ADAPT_STRUCT(TYPE,__VA_ARGS__) \
NOT_FUSION_SAVE_OFFSETS(TYPE,BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))

struct test1 {
    int a;  
    char c;  
    double b;  
};

struct test2 {
    char c;
    short s;
    int i;
};

ADAPT_STRUCT_AND_SAVE_OFFSETS(test1,a,c,b);
ADAPT_STRUCT_AND_SAVE_OFFSETS(test2,c,s,i);


template <typename Struct>
void print_offsets(const std::string& name,const Struct&)
{
    //This could be changed to use 'std::integer_sequence' instead of mpl
    std::cout << "Offsets for " << name << ":" << std::endl;
    typedef boost::mpl::range_c<unsigned, 0, boost::fusion::result_of::size<Struct>::value > Indices; 
    boost::fusion::for_each(Indices(),
        [](auto index)
        { 
            std::cout << boost::fusion::extension::struct_member_name<Struct,index>::call() << " -> " << not_fusion::offset_of<Struct,index>::value << std::endl;
        }
    );
}

int main() {
    test1 t1{1, 2, 3.0}; 
    test2 t2{1,2,3};

    print_offsets("test1",t1);
    print_offsets("test2",t2);
}
llonesmiz
  • 155
  • 2
  • 11
  • 20