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

Вот ядро счётчика:

int engine(...);

template<bool f> struct ts
{
    typedef int type;
};

template<> struct ts<false>
{
    typedef char type;
};
 
#ifndef COUNTER_MAX_COUNT
#define COUNTER_MAX_COUNT 100
#endif

#define COUNTER_HELPER(z, n, text) , typename ts<(v>n)>::type

template<typename type>
struct cnt_unique {};

template<typename, template <int> class, int> struct cnt;
typedef cnt<cnt_unique<cnt_unique<void> >, lin_gen, 0> cntt;

template
<
    typename tag_t = void, 
    template <int> class gen_t = lin_gen,
    int id = sizeof(engine(*(cntt*)0, *(cnt_unique<tag_t>*)0, 
        BOOST_PP_ENUM_PARAMS(COUNTER_MAX_COUNT, (int))))
>
struct cnt
{
    static const int v = id / 4 - 1;
    friend int(&engine(cntt&, cnt_unique<tag_t>& BOOST_PP_REPEAT(COUNTER_MAX_COUNT, COUNTER_HELPER, _)))[v+2];
    static const int val = gen_t<v>::val;
};



Вот генераторы (свой добавить тривиально):

template<int id> struct lin_gen 
{
    static const int val = id;
};

template<int id> struct even_gen 
{
    static const int val = id * 2;
};

template<int id> struct lin_gen_down
{
    static const int val = -id;
};

template<int id> struct pow2_gen
{
    static const int val = 2 << id;
};



Вот функции получения текущего значения счётчика и прибавления числа к счётчику:

template
<
    typename tag_t = void, 
    template <int> class gen_t = lin_gen,
    int id = sizeof(engine(*(cntt*)0, *(cnt_unique<tag_t>*)0, 
        BOOST_PP_ENUM_PARAMS(COUNTER_MAX_COUNT, (int))))
>
struct cnt_cur
{
    static const int v = id / 4 - 1;
    static const int val = gen_t<v>::val;
};

template
<
    int id,
    unsigned int add,
    typename tag_t, 
    template <int> class gen_t
>
struct cnt_add_helper : cnt_add_helper<id, add - 1, tag_t, gen_t>
{
    static const int val = cnt<tag_t, gen_t>::val;
};

template
<
    int id,
    typename tag_t, 
    template <int> class gen_t
>
struct cnt_add_helper<id, 1, tag_t, gen_t>
{};

template
<
    unsigned int add,
    typename tag_t = void, 
    template <int> class gen_t = lin_gen,
    int id = cnt<>::val
>
struct cnt_add : cnt_add_helper<id, add, tag_t, gen_t>
{
    static const int val = cnt<tag_t, gen_t>::val;
};



Можно ещё сделать возможность декремента и вычитания для счётчика. Примерно таким макаром:

template
<
    typename tag_t = void, 
    template <int> class gen_t = lin_gen,
    int id = sizeof(engine(*(cntt*)0, *(cnt_unique<tag_t>*)0, 
        BOOST_PP_ENUM_PARAMS(COUNTER_MAX_COUNT, (int))))
        - sizeof(engine2(*(cntt*)0, *(cnt_unique<tag_t>*)0, 
        BOOST_PP_ENUM_PARAMS(COUNTER_MAX_COUNT, (int))))
>
struct cnt ...


Тогда получится настоящий ct_var. Который можно использовать так:

struct my_var;
ct_var<my_var>::val;
ct_var<my_var>::inc::val;
ct_var<my_var>::dec::val;
ct_var<my_var>::add<5>::val;
ct_var<my_var>::sub<5>::val;



1024cores &mdash; all about multithreading, multicore, concurrency, parallelism, lock-free algorithms
Re: [Trick] Компайл-тайм счётчик
От: Roman Odaisky Украина  
Дата: 06.02.07 21:54
Оценка: +1 :))) :)))
Не везет мне с твоими изобретениями...

Приведу это полностью

tmp.cpp:55:1: pasting ")" and "0" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "1" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "2" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "3" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "4" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "5" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "6" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "7" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "8" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "9" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "10" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "11" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "12" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "13" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "14" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "15" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "16" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "17" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "18" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "19" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "20" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "21" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "22" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "23" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "24" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "25" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "26" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "27" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "28" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "29" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "30" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "31" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "32" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "33" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "34" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "35" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "36" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "37" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "38" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "39" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "40" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "41" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "42" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "43" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "44" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "45" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "46" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "47" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "48" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "49" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "50" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "51" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "52" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "53" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "54" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "55" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "56" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "57" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "58" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "59" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "60" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "61" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "62" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "63" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "64" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "65" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "66" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "67" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "68" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "69" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "70" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "71" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "72" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "73" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "74" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "75" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "76" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "77" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "78" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "79" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "80" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "81" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "82" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "83" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "84" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "85" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "86" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "87" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "88" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "89" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "90" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "91" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "92" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "93" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "94" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "95" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "96" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "97" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "98" does not give a valid preprocessing token
tmp.cpp:55:1: pasting ")" and "99" does not give a valid preprocessing token
tmp.cpp:60: warning: friend declaration `int (& engine(cntt&, cnt_unique<tag_t>&, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 0)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 1)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 2)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 3)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 4)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 5)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 6)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 7)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 8)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 9)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 10)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 11)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 12)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 13)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 14)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 15)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 16)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 17)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 18)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 19)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 20)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 21)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 22)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 23)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 24)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 25)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 26)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 27)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 28)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 29)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 30)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 31)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 32)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 33)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 34)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 35)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 36)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 37)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 38)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 39)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 40)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 41)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 42)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 43)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 44)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 45)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 46)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 47)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 48)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 49)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 50)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 51)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 52)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 53)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 54)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 55)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 56)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 57)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 58)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 59)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 60)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 61)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 62)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 63)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 64)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 65)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 66)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 67)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 68)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 69)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 70)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 71)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 72)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 73)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 74)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 75)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 76)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 77)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 78)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 79)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 80)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 81)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 82)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 83)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 84)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 85)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 86)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 87)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 88)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 89)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 90)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 91)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 92)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 93)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 94)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 95)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 96)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 97)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 98)>::type, typename ts<(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v > 99)>::type))[(cnt< <template-parameter-1-1>, <template-parameter-1-2>, <anonymous> >::v + 2)]' declares a non-template function
tmp.cpp:60: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
tmp.cpp:69:1: pasting ")" and "0" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "1" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "2" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "3" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "4" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "5" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "6" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "7" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "8" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "9" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "10" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "11" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "12" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "13" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "14" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "15" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "16" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "17" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "18" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "19" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "20" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "21" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "22" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "23" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "24" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "25" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "26" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "27" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "28" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "29" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "30" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "31" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "32" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "33" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "34" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "35" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "36" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "37" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "38" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "39" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "40" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "41" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "42" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "43" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "44" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "45" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "46" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "47" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "48" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "49" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "50" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "51" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "52" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "53" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "54" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "55" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "56" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "57" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "58" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "59" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "60" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "61" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "62" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "63" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "64" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "65" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "66" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "67" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "68" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "69" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "70" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "71" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "72" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "73" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "74" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "75" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "76" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "77" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "78" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "79" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "80" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "81" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "82" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "83" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "84" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "85" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "86" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "87" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "88" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "89" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "90" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "91" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "92" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "93" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "94" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "95" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "96" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "97" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "98" does not give a valid preprocessing token
tmp.cpp:69:1: pasting ")" and "99" does not give a valid preprocessing token

=== Compile failed.
До последнего не верил в пирамиду Лебедева.
Re[2]: [Trick] Компайл-тайм счётчик
От: remark Россия http://www.1024cores.net/
Дата: 06.02.07 22:32
Оценка: :)
Здравствуйте, Roman Odaisky, Вы писали:

RO>Не везет мне с твоими изобретениями...


RO>Приведу это полностью


RO>=== Compile failed.


Чем компилил-то?
Попробуй в макросе COUNTER_HELPER поставить побольше пробелов, что бы разделить лексемы...


1024cores &mdash; all about multithreading, multicore, concurrency, parallelism, lock-free algorithms
Re: [Trick] Компайл-тайм счётчик
От: IROV..  
Дата: 06.02.07 22:57
Оценка: 36 (5)
char engine( ... );

template<int I>
struct _number : public _number< I - 1 >{};

template<>
struct _number<0>{};

template<int id = sizeof( engine( _number<100>() ) )>
struct _counter
{
    friend char(&engine( _number<id + 1> ))[ id + 1 ];
    static const int val = id - 1;
};

int main()
{
    printf("%d", _counter<>::val );
    printf("%d", _counter<>::val );
    printf("%d", _counter<>::val );
}


Будь проще!
я не волшебник, я только учусь!
Re: а что означает char(&engine( int ))[10]???
От: avs99 Южная Корея  
Дата: 07.02.07 04:21
Оценка:
Здравствуйте, remark, Вы писали:

R>В продолжение темы Невероятно, но факт! Не константные значения в компайл-тайм!
Автор: remark
Дата: 06.02.07
. Обещанный компайл-тайм счётчик.


ОЧень интересно, особенно про случайные значения! Спасибо большое. Только вот такой вопрос по синтаксису, что означает (и как расшифровать) описание функции engine():

char(&engine( int ))[10];



Я так понял, что это объявляется функция с именем engine, принимающая int и возвращающая массив 10 ссылок на char?? но ведь массивы ж возвращать нельзя.. или это ссылка на массив размером 10?? В общем, буду благодарен за объяснение, что это.. В гугле сходу не нашел

Спасибо,

Алексей
Re[2]: а что означает char(&engine( int ))[10]???
От: avs99 Южная Корея  
Дата: 07.02.07 05:17
Оценка: 2 (1)
A>ОЧень интересно, особенно про случайные значения! Спасибо большое. Только вот такой вопрос по синтаксису, что означает (и как расшифровать) описание функции engine():

A>
A>char(&engine( int ))[10];
A>


A>Я так понял, что это объявляется функция с именем engine, принимающая int и возвращающая массив 10 ссылок на char?? но ведь массивы ж возвращать нельзя.. или это ссылка на массив размером 10?? В общем, буду благодарен за объяснение, что это.. В гугле сходу не нашел


Это функция, которая принимает int и возвращает ссылку на одномерный массив с заданным во время компиляции размером. О как Это, вроде бы, единственное "исключение" из правила, что функция не может возвращать массив. Ни разу с таким чудом не сталкивался

и вот маленький примерчик для закрепления

const int size = 100;

// функция add_value, которая принимает ссылку на массив из SIZE (константа) элементов,
// складывает каждый элемент с заданным числом,
// и возвращает ссылку на этот же массив из SIZE (константа) элементов
int ( &add_value(int (&arr)[size], int value) )[size]
{
    for (int i = 0; i < size; i++)
        arr[i] += value;

    return arr;
}

int main()
{
    // объявили и инициализировали массив
    int arr[size] = {};

    // добавили к каждому элементу массива 123 и вернули ссылку на этот же массив.
    // arr и arr_ref - суть одно и тоже
    int (&arr_ref)[size] = add_value(arr, 123);

    return 0;
}



Алексей
Re[2]: [Trick] Компайл-тайм счётчик
От: Andrew S Россия http://alchemy-lab.com
Дата: 07.02.07 10:01
Оценка:
IRO>Будь проще!

На 7.1 работает, а вот на 6-ку говорит: error C2265: '<Unknown>' : reference to a zero-sized array is illegal

Можно обойтись таким вариантом:


template <size_t nSize> 
struct sizer
{
    char tmp[nSize];
};

sizer<1> engine( ... );

template<int I>
struct _number : public _number< I - 1 >{};

template<>
struct _number<0>{};

template<int id = sizeof( engine( _number<100>() ) )>
struct _counter
{
    friend sizer<id + 1> engine( _number<id + 1> );
    enum {val = id - 1};
};


Тогда будет работать почти везде.
Но тут ограничение счетчика до 100, которое, собственно, является ограничением на вложенность инстанцирования _number. Как бы это дело обойти, а то можно будет typeof на VC6 придумать
http://www.rusyaz.ru/pr — стараемся писАть по-русски
Re[3]: [Trick] Компайл-тайм счётчик
От: IROV..  
Дата: 07.02.07 11:31
Оценка:
Здравствуйте, Andrew S, Вы писали:

IRO>>Будь проще!


AS>На 7.1 работает, а вот на 6-ку говорит: error C2265: '<Unknown>' : reference to a zero-sized array is illegal


AS>Можно обойтись таким вариантом:



AS>
AS>template <size_t nSize> 
AS>struct sizer
AS>{
AS>    char tmp[nSize];
AS>};

AS>sizer<1> engine( ... );

AS>template<int I>
AS>struct _number : public _number< I - 1 >{};

AS>template<>
AS>struct _number<0>{};

AS>template<int id = sizeof( engine( _number<100>() ) )>
AS>struct _counter
AS>{
AS>    friend sizer<id + 1> engine( _number<id + 1> );
AS>    enum {val = id - 1};
AS>};


AS>


AS>Тогда будет работать почти везде.

AS>Но тут ограничение счетчика до 100, которое, собственно, является ограничением на вложенность инстанцирования _number. Как бы это дело обойти, а то можно будет typeof на VC6 придумать

для VC6 и VC7 уже вчера ночью написал, чекал реализацию Чеза.
я не волшебник, я только учусь!
Re: [Trick] Компайл-тайм счётчик
От: Pavel Chikulaev Россия  
Дата: 07.02.07 11:34
Оценка: 26 (4)
Здравствуйте, remark, Вы писали:

Copyright 2001 (c) Alexey Gurtovoy:

http://lists.boost.org/Archives/boost/2002/10/38298.php
Re[4]: [Trick] Компайл-тайм счётчик
От: Andrew S Россия http://alchemy-lab.com
Дата: 07.02.07 11:37
Оценка:
IRO>для VC6 и VC7 уже вчера ночью написал, чекал реализацию Чеза.

У меня тоже есть вариант этой (http://gzip.rsdn.ru/Forum/Message.aspx?mid=1094305&amp;only=1
Автор: Chez
Дата: 28.03.05
) реализации под VC6/VC7.
Вот тут (http://gzip.rsdn.ru/File/8583/typeof.zip) лежит. Интересно, на VC8 эти 2 багофичи работают, или уже нет
И ограничение сверху для счетчика все-таки напрягает.
http://www.rusyaz.ru/pr — стараемся писАть по-русски
Re[2]: [Trick] Компайл-тайм счётчик
От: Vain Россия google.ru
Дата: 07.02.07 17:15
Оценка:
Здравствуйте, Pavel Chikulaev, Вы писали:

PC>Copyright 2001 (c) Alexey Gurtovoy:

PC>http://lists.boost.org/Archives/boost/2002/10/38298.php
Круто и на comeau 4.3.3 работает!
[In theory there is no difference between theory and practice. In
practice there is.]
[Даю очевидные ответы на риторические вопросы]
Re[3]: [Trick] Компайл-тайм счётчик
От: Roman Odaisky Украина  
Дата: 07.02.07 21:52
Оценка:
Здравствуйте, remark, Вы писали:

RO>>=== Compile failed.


R>Чем компилил-то?


gcc это, «Compile failed» выдает скрипт.

R>Попробуй в макросе COUNTER_HELPER поставить побольше пробелов, что бы разделить лексемы...


Я думаю, там где-то лишние ##.
До последнего не верил в пирамиду Лебедева.
Re[4]: [Trick] Компайл-тайм счётчик
От: remark Россия http://www.1024cores.net/
Дата: 08.02.07 07:56
Оценка:
Здравствуйте, Roman Odaisky, Вы писали:

RO>Здравствуйте, remark, Вы писали:


RO>>>=== Compile failed.


R>>Чем компилил-то?


msvc71/80

RO>gcc это, «Compile failed» выдает скрипт.


R>>Попробуй в макросе COUNTER_HELPER поставить побольше пробелов, что бы разделить лексемы...


RO>Я думаю, там где-то лишние ##.



Я тут о высоком, а вы со своими ошибками компиляции спускаете меня на землю
Вот, держи вывод моего препроцессора:


template
<
    typename tag_t = void, 
    template <int> class gen_t = lin_gen,
    int id = sizeof(engine(*(cntt*)0, *(cnt_unique<tag_t>*)0, 
         (int)0 , (int)1 , (int)2 , (int)3 , (int)4 , (int)5 , (int)6 , (int)7 , (int)8 , (int)9 , (int)10 , (int)11 , (int)12 , (int)13 , (int)14 , (int)15 , (int)16 , (int)17 , (int)18 , (int)19 , (int)20 , (int)21 , (int)22 , (int)23 , (int)24 , (int)25 , (int)26 , (int)27 , (int)28 , (int)29 , (int)30 , (int)31 , (int)32 , (int)33 , (int)34 , (int)35 , (int)36 , (int)37 , (int)38 , (int)39 , (int)40 , (int)41 , (int)42 , (int)43 , (int)44 , (int)45 , (int)46 , (int)47 , (int)48 , (int)49 , (int)50 , (int)51 , (int)52 , (int)53 , (int)54 , (int)55 , (int)56 , (int)57 , (int)58 , (int)59 , (int)60 , (int)61 , (int)62 , (int)63 , (int)64 , (int)65 , (int)66 , (int)67 , (int)68 , (int)69 , (int)70 , (int)71 , (int)72 , (int)73 , (int)74 , (int)75 , (int)76 , (int)77 , (int)78 , (int)79 , (int)80 , (int)81 , (int)82 , (int)83 , (int)84 , (int)85 , (int)86 , (int)87 , (int)88 , (int)89 , (int)90 , (int)91 , (int)92 , (int)93 , (int)94 , (int)95 , (int)96 , (int)97 , (int)98 , (int)99))
>
struct cnt
{
    static const int v = id / 4 - 1;
    friend int(&engine(cntt&, cnt_unique<tag_t>& , typename ts<(v>0)>::type , typename ts<(v>1)>::type , typename ts<(v>2)>::type , typename ts<(v>3)>::type , typename ts<(v>4)>::type , typename ts<(v>5)>::type , typename ts<(v>6)>::type , typename ts<(v>7)>::type , typename ts<(v>8)>::type , typename ts<(v>9)>::type , typename ts<(v>10)>::type , typename ts<(v>11)>::type , typename ts<(v>12)>::type , typename ts<(v>13)>::type , typename ts<(v>14)>::type , typename ts<(v>15)>::type , typename ts<(v>16)>::type , typename ts<(v>17)>::type , typename ts<(v>18)>::type , typename ts<(v>19)>::type , typename ts<(v>20)>::type , typename ts<(v>21)>::type , typename ts<(v>22)>::type , typename ts<(v>23)>::type , typename ts<(v>24)>::type , typename ts<(v>25)>::type , typename ts<(v>26)>::type , typename ts<(v>27)>::type , typename ts<(v>28)>::type , typename ts<(v>29)>::type , typename ts<(v>30)>::type , typename ts<(v>31)>::type , typename ts<(v>32)>::type , typename ts<(v>33)>::type , typename ts<(v>34)>::type , typename ts<(v>35)>::type , typename ts<(v>36)>::type , typename ts<(v>37)>::type , typename ts<(v>38)>::type , typename ts<(v>39)>::type , typename ts<(v>40)>::type , typename ts<(v>41)>::type , typename ts<(v>42)>::type , typename ts<(v>43)>::type , typename ts<(v>44)>::type , typename ts<(v>45)>::type , typename ts<(v>46)>::type , typename ts<(v>47)>::type , typename ts<(v>48)>::type , typename ts<(v>49)>::type , typename ts<(v>50)>::type , typename ts<(v>51)>::type , typename ts<(v>52)>::type , typename ts<(v>53)>::type , typename ts<(v>54)>::type , typename ts<(v>55)>::type , typename ts<(v>56)>::type , typename ts<(v>57)>::type , typename ts<(v>58)>::type , typename ts<(v>59)>::type , typename ts<(v>60)>::type , typename ts<(v>61)>::type , typename ts<(v>62)>::type , typename ts<(v>63)>::type , typename ts<(v>64)>::type , typename ts<(v>65)>::type , typename ts<(v>66)>::type , typename ts<(v>67)>::type , typename ts<(v>68)>::type , typename ts<(v>69)>::type , typename ts<(v>70)>::type , typename ts<(v>71)>::type , typename ts<(v>72)>::type , typename ts<(v>73)>::type , typename ts<(v>74)>::type , typename ts<(v>75)>::type , typename ts<(v>76)>::type , typename ts<(v>77)>::type , typename ts<(v>78)>::type , typename ts<(v>79)>::type , typename ts<(v>80)>::type , typename ts<(v>81)>::type , typename ts<(v>82)>::type , typename ts<(v>83)>::type , typename ts<(v>84)>::type , typename ts<(v>85)>::type , typename ts<(v>86)>::type , typename ts<(v>87)>::type , typename ts<(v>88)>::type , typename ts<(v>89)>::type , typename ts<(v>90)>::type , typename ts<(v>91)>::type , typename ts<(v>92)>::type , typename ts<(v>93)>::type , typename ts<(v>94)>::type , typename ts<(v>95)>::type , typename ts<(v>96)>::type , typename ts<(v>97)>::type , typename ts<(v>98)>::type , typename ts<(v>99)>::type))[v+2];
    static const int val = gen_t<v>::val;
};



1024cores &mdash; all about multithreading, multicore, concurrency, parallelism, lock-free algorithms
Re[2]: [Trick] Компайл-тайм счётчик
От: remark Россия http://www.1024cores.net/
Дата: 08.02.07 07:57
Оценка:
Здравствуйте, IROV.., Вы писали:

IRO>Будь проще!


Вариант, в принципе, хороший. Только при чём тут проще? И вообще это уже детали.


1024cores &mdash; all about multithreading, multicore, concurrency, parallelism, lock-free algorithms
Re[2]: [Trick] Компайл-тайм счётчик
От: remark Россия http://www.1024cores.net/
Дата: 08.02.07 08:03
Оценка:
Здравствуйте, Pavel Chikulaev, Вы писали:

PC>Здравствуйте, remark, Вы писали:


PC>Copyright 2001 (c) Alexey Gurtovoy:


PC>http://lists.boost.org/Archives/boost/2002/10/38298.php



... да, всё действительно уже украдено до нас.
Но тоже как Chez явно не увидел всего потенциала


1024cores &mdash; all about multithreading, multicore, concurrency, parallelism, lock-free algorithms
Re[5]: [Trick] Компайл-тайм счётчик
От: remark Россия http://www.1024cores.net/
Дата: 08.02.07 08:13
Оценка:
Здравствуйте, Andrew S, Вы писали:

AS> Интересно, на VC8 эти 2 багофичи работают, или уже нет


Под 8-ку здесь
Автор:
Дата: 26.12.06



1024cores &mdash; all about multithreading, multicore, concurrency, parallelism, lock-free algorithms
Re: [Trick] Компайл-тайм счётчик
От: int13h Украина  
Дата: 08.02.07 10:53
Оценка:
Здравствуйте, remark, Вы писали:

R>В продолжение темы [url=http://gzip.rsdn.ru/Forum/Message.aspx?mid=2337951&amp;only=1
Автор: remark
Дата: 06.02.07
]Невероятно, но факт!

[потеряно ]

Спасибо, интересно. Хорошие ресерчи пишешь
Re[3]: [Trick] Компайл-тайм счётчик
От: IROV..  
Дата: 08.02.07 17:58
Оценка:
Здравствуйте, remark, Вы писали:

R>Вариант, в принципе, хороший. Только при чём тут проще? И вообще это уже детали.


R>


Этот вариант для тех кто не _любит_ boost

BOOST_PP_ENUM_PARAMS
BOOST_PP_REPEAT


etc



То что детали, бузуслово шлифовка идеии так сказать
я не волшебник, я только учусь!
Re[2]: [Trick] Компайл-тайм счётчик
От: Шахтер Интернет  
Дата: 08.02.07 21:03
Оценка:
Здравствуйте, Pavel Chikulaev, Вы писали:

PC>Здравствуйте, remark, Вы писали:


PC>Copyright 2001 (c) Alexey Gurtovoy:


PC>http://lists.boost.org/Archives/boost/2002/10/38298.php


По моему это толже не верно.


    #include "boost/static_assert.hpp" 
    #include "boost/config.hpp" 


    typedef char (&no_tag)[1]; 
    typedef char (&yes_tag)[2]; 


    template< typename U > no_tag test(U*, ...); 
    template< typename T, long > struct instantiation_count; 


    template< 
          typename T 
        , long value_ = 
              sizeof(test(static_cast<instantiation_count<T,1>*>(0))) 
            + sizeof(test(static_cast<instantiation_count<T,2>*>(0))) 
            + sizeof(test(static_cast<instantiation_count<T,3>*>(0))) 
            + sizeof(test(static_cast<instantiation_count<T,4>*>(0))) 
            + sizeof(test(static_cast<instantiation_count<T,5>*>(0))) 
> 
    struct instantiation_count 
    { 
        BOOST_STATIC_CONSTANT(long, value = value_ - 5 + 1); 
        friend yes_tag test(instantiation_count<T, value>*); 
    }; 


    // here! 

    Здесь компилятор вставит инстантинации (во всяком случае Вандервуд совершенно недвусмысленно об этом говорит)

    struct instantiation_count<T,1>;
    struct instantiation_count<T,2>;
    struct instantiation_count<T,3>;
    struct instantiation_count<T,4>;
    struct instantiation_count<T,5>;

    После этого он будет считать второй параметр шаблона
 
    long value_ = 
              sizeof(test(static_cast<instantiation_count<T,1>*>(0)))   это равно 2 -- вызов test c параметром типа instantiation_count<T,1> *
                                                                        найдет по ADL instantiation_count<T,1>::test(instantiation_count<T,1> *) 

            + sizeof(test(static_cast<instantiation_count<T,2>*>(0))) и это равно 2
            + sizeof(test(static_cast<instantiation_count<T,3>*>(0))) и это равно 2 
            + sizeof(test(static_cast<instantiation_count<T,4>*>(0))) и это равно 2
            + sizeof(test(static_cast<instantiation_count<T,5>*>(0))) и это равно 2


    BOOST_STATIC_ASSERT(instantiation_count<int>::value == 1); // хрен там == 6
    BOOST_STATIC_ASSERT(instantiation_count<int>::value == 2); // == 6
    BOOST_STATIC_ASSERT(instantiation_count<int>::value == 3); // == 6
    BOOST_STATIC_ASSERT(instantiation_count<int>::value == 4); // == 6
    BOOST_STATIC_ASSERT(instantiation_count<int>::value == 5); // == 6
    BOOST_STATIC_ASSERT(instantiation_count<int>::value == 6); // == 6


    BOOST_STATIC_ASSERT(instantiation_count<long>::value == 1); 
    BOOST_STATIC_ASSERT(instantiation_count<short>::value == 1); 
    BOOST_STATIC_ASSERT(instantiation_count<long>::value == 2);
В XXI век с CCore.
Копай Нео, копай -- летать научишься. © Matrix. Парадоксы
Re[3]: [Trick] Компайл-тайм счётчик
От: night beast СССР  
Дата: 09.02.07 06:27
Оценка:
Здравствуйте, Шахтер, Вы писали:

Ш>    #include "boost/static_assert.hpp" 
Ш>    #include "boost/config.hpp" 

Ш>    typedef char (&no_tag)[1]; 
Ш>    typedef char (&yes_tag)[2]; 

Ш>    template< typename U > no_tag test(U*, ...); 
Ш>    template< typename T, long > struct instantiation_count; 

Ш>    template< 
Ш>          typename T 
Ш>        , long value_ = 
Ш>              sizeof(test(static_cast<instantiation_count<T,1>*>(0))) 
Ш>            + sizeof(test(static_cast<instantiation_count<T,2>*>(0))) 
Ш>            + sizeof(test(static_cast<instantiation_count<T,3>*>(0))) 
Ш>            + sizeof(test(static_cast<instantiation_count<T,4>*>(0))) 
Ш>            + sizeof(test(static_cast<instantiation_count<T,5>*>(0))) 
>> 
Ш>    struct instantiation_count 
Ш>    { 
Ш>        BOOST_STATIC_CONSTANT(long, value = value_ - 5 + 1); 
Ш>        friend yes_tag test(instantiation_count<T, value>*); 
Ш>    }; 


Ш>    // here! 

Ш>    Здесь компилятор вставит инстантинации (во всяком случае Вандервуд совершенно недвусмысленно об этом говорит)

Ш>    struct instantiation_count<T,1>;
Ш>    struct instantiation_count<T,2>;
Ш>    struct instantiation_count<T,3>;
Ш>    struct instantiation_count<T,4>;
Ш>    struct instantiation_count<T,5>;

Ш>    После этого он будет считать второй параметр шаблона
 
Ш>    long value_ =


правильно ли я тебя понял, что при вычислении default параметра (в месте использования) компилятор должет провести все инстанциации применяемых типов.
даже те, которые в обычной ситуации проводить не нужно?
если правильно, то в какой главе? (что-то не нашел )
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.