Unerstand how to compute sum at compiletime
I have this piece of code and want to understand:
template <unsigned...>
struct sum;
template<unsigned size>
struct sum<size>
{
enum {value = size};
};
template<unsigned size, unsigned... sizes>
struct sum<size, sizes...>
{
enum { value = size + sum<sizes...>::value };
};
int _tmain(int argc, _TCHAR* argv[])
{
sum<1, 2>::value;
return 0;
}
I don't understand why the unimplemented sum(that takes unsigned... just
as last struct specialization, isn't there a conflict?) must be present at
all and how one can specialize sum using same parameters from template
part (e.g. sum<size, sizes...> same as template <unsigned size, sizes...>.
Why the bellow just doesn't work ?
template<unsigned size>
struct sum
{
enum {value = size};
};
template<unsigned size, unsigned... sizes>
struct sum
{
enum { value = size + sum<sizes...>::value; };
};
No comments:
Post a Comment