[Trick] Автоматическое определение смещения
От: remark Россия http://www.1024cores.net/
Дата: 08.02.07 08:08
Оценка: 7 (2)
В продолжение темы Невероятно, но факт! Не константные значения в компайл-тайм!
Автор: remark
Дата: 06.02.07
. Обещанное Автоматическое определение смещения.

Что бы скомпилировалось надо подключить компийл-тайм счётчик.

template<unsigned int cur_size, unsigned int cur_offset>
struct offset_offsetter
{
    static const unsigned int val = cur_offset + (cur_offset % 4 ? (4 - cur_offset % 4) : 0);
};

template<unsigned int cur_offset>
struct offset_offsetter<2, cur_offset>
{
    static const unsigned int val = cur_offset + cur_offset % 2;
};

template<unsigned int cur_offset>
struct offset_offsetter<1, cur_offset>
{
    static const unsigned int val = cur_offset;
};

template<typename type>
struct prop_cont
{
    type value_;
};

template<int id, typename cont_t, typename type, unsigned int offset>
struct prop_base : prop_cont<type>, cnt_add<sizeof(prop_cont<type>) + offset - cnt_cur<cont_t>::val, cont_t>
{
    static const unsigned int offset = offset;
};

template<typename cont_t, typename type, int id = cnt<>::val>
struct prop : prop_base<id, cont_t, type, offset_offsetter<sizeof(prop_cont<type>), cnt_cur<cont_t>::val>::val>
{
};

struct person
{
    prop<person, int> m1;
    prop<person, char> m2;
    prop<person, std::string> m3;
    prop<person, char> m4;
    prop<person, int> m5;
};


Каждый член в мембере offset имеет свой смещение относительно контейнера.


1024cores &mdash; all about multithreading, multicore, concurrency, parallelism, lock-free algorithms
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.