In the code below (C ++ 14, no 'fold' from C ++ 17), I am trying to automatically calculate fixed offsets for a class field at compile time using merge merge acceleration, parameter packages and lambda. Unfortunately, this leads to a compile-time error ... Is it possible to do something like this?
[EDIT: something else bothers me: it's not quite what I want. I would like _size from ControlledLayout2 to be available at compile time (why I made it static), and not just when calling the constructor]
template <typename T, uint32_t size> struct Field2 { typedef T _type; static const uint32_t _size; static uint32_t _offset; }; template <typename T, uint32_t size> const uint32_t Field2<T,size>::_size = size; template <typename T, uint32_t size> uint32_t Field2<T,size>::_offset = 0; template <typename ... T> struct ControlledLayout2 { static uint32_t _size; ControlledLayout2(T... args) { _size = fold({args...}, 0, [&](uint32_t s, T field) { return T::_offset = s + T::_size; }...); }; }; ... ControlledLayout2<Field2<int, 32>, Field2<char, 1>, Field2<long, 64>> cl; cout << cl._size << endl; ...
And the compiler error:
error: parameter not expanded with '...'; _size = accumulate({args...}, ...
source share