In the code below (C++14, no 'fold' from C++17), I am trying to automatically compute fixed offsets for the field of a class, at compile time, using boost fusion fold, parameter packs and a lambda. Unfortunately, this results in a compile time error... Is it possible to do something like this?
[EDIT: something else bothers me too: this is not exactly what I want. I would like the _size of ControlledLayout2 to be available at compile time (that's why I made it static), not just when the constructor will be called]
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 is:
error: parameter not expanded with '...';
_size = accumulate({args...}, ...
T field
->T
is not expanded – Piotr Skotnicki...
after}
closing the lambda expression (though it fails in GCC) – Piotr Skotnicki