私は、テンプレートのメタプログラミングについて少し学びたいと思っています。以下のコードでは、コンパイル時に指定されたNビットを保持するのに十分な大きさの符号なし整数型をいくつかのテンプレート再帰を使用して検索しようとしています。C++の再帰的なテンプレートタイプの控除
template <typename T>
struct NextIntegralType
{
};
template <>
struct NextIntegralType<void>
{
typedef unsigned char type;
};
template <>
struct NextIntegralType<unsigned char>
{
typedef unsigned short type;
};
...More type 'iteration' here...
template<size_t BITS, typename T>
struct FindIntegralType2
{
typedef std::conditional<BITS <= sizeof(typename T::type)*8, T, FindIntegralType2<BITS, NextIntegralType<typename T::type>>> _type;
typedef typename _type::type type;
};
template<size_t BITS>
struct FindIntegralType
{
typedef typename FindIntegralType2<BITS, NextIntegralType<void>>::type type;
};
私は変数を宣言し、それに整数値を割り当てる...
FindIntegralType<15>::type test(4000);
私は、次を得る:
error: no matching function for call to ‘FindIntegralType2<15u, NextIntegralType<unsigned char> >::FindIntegralType2(int)’
note: candidates are:
note: constexpr FindIntegralType2<15u, NextIntegralType<unsigned char> >::FindIntegralType2()
note: candidate expects 0 arguments, 1 provided
note: constexpr FindIntegralType2<15u, NextIntegralType<unsigned char> >::FindIntegralType2(const FindIntegralType2<15u, NextIntegralType<unsigned char> >&)
note: no known conversion for argument 1 from ‘int’ to ‘const FindIntegralType2<15u, NextIntegralType<unsigned char> >&’
私の再帰は 'されていないように思え解き放つ。誰かが私を正しい方向に向けることができますか?
注:私はGCC 4.6を使用しています。
EDIT:ブースト(彼らは常にある)で答えを指し示す
Automatically pick a variable type big enough to hold a specified number
私は私が前に逃した記事を見つけましたこれは私の実用的な必要性と知的好奇心の両方を解決するはずです。
なぜstdint.hを使用しないのですか? – mfontanini
@mfontaniniあなたは詳しく説明できますか? – TractorPulledPork