Мне интересно немного узнать о метапрограммировании шаблонов. В приведенном ниже коде я пытаюсь найти целочисленный тип без знака, достаточно большой для хранения N битов, указанных во время компиляции, используя некоторую рекурсию шаблона.
template
struct NextIntegralType
{
};
template <>
struct NextIntegralType
{
typedef unsigned char type;
};
template <>
struct NextIntegralType
{
typedef unsigned short type;
};
...More type 'iteration' here...
template
struct FindIntegralType2
{
typedef std::conditional>> _type;
typedef typename _type::type type;
};
template
struct FindIntegralType
{
typedef typename FindIntegralType2>::type type;
};
Когда я объявляю переменную и присваиваю ей целочисленное значение...
FindIntegralType<15>::type test(4000);
Я получаю следующее:
error: no matching function for call to ‘FindIntegralType2<15u, NextIntegralType >::FindIntegralType2(int)’
note: candidates are:
note: constexpr FindIntegralType2<15u, NextIntegralType >::FindIntegralType2()
note: candidate expects 0 arguments, 1 provided
note: constexpr FindIntegralType2<15u, NextIntegralType >::FindIntegralType2(const FindIntegralType2<15u, NextIntegralType >&)
note: no known conversion for argument 1 from ‘int’ to ‘const FindIntegralType2<15u, NextIntegralType >&’
Кажется, моя рекурсия не "раскручивается". Может кто-то указать мне верное направление?
Примечание. Я использую GCC 4.6.
РЕДАКТИРОВАТЬ:
Я нашел пост, который пропустил раньше:
Автоматически выбирать тип переменной, достаточно большой, чтобы вместить указанное число
Что указывает на ответ в boost (где они всегда есть):
boost_integer
Это должно решить как мою практическую потребность, так и интеллектуальное любопытство.