Skip to content

Instantly share code, notes, and snippets.

@ttldtor
Last active December 14, 2015 21:19
Show Gist options
  • Save ttldtor/5150219 to your computer and use it in GitHub Desktop.
Save ttldtor/5150219 to your computer and use it in GitHub Desktop.
Как бы хотелось вместо (1) иметь возможность делать (2) или (3).
/* 1 */
template<template <size_t> class T, size_t... Indices> struct summa;
template<template <size_t> class T, size_t Index, size_t... Indices>
struct summa<T, Index, Indices...>
{
static const decltype(T<Index>::value) value;
};
template<template <size_t> class T, size_t Index, size_t... Indices>
const decltype(T<Index>::value) summa<T, Index, Indices...>::value = T<Index>::value + summa<T, Indices...>::value;
template<template <size_t> class T, size_t Index>
struct summa<T, Index>
{
static const decltype(T<Index>::value) value;
};
template<template <size_t> class T, size_t Index>
const decltype(T<Index>::value) summa<T, Index>::value = T<Index>::value;
/* 2 */
template<Signature>
struct summa
{
static switch (template Signature)
{
case template <size_t> class T, size_t Index, size_t... Indices:
static constexpr auto value = T<Index>::value + summa<T, Indices...>::value;
break;
case template <size_t> class T, size_t Index:
static constexpr auto value = T<Index>::value;
break;
default:;
}
};
/* 3 */
template<template <size_t> class T, size_t Index, size_t... Indices>
struct summa
{
static if (sizeof...(Indices) > 0)
static constexpr auto value = T<Index>::value + summa<T, Indices...>::value;
else
static constexpr auto value = T<Index>::value;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment