递归地推导缺失模板特化的值类型
Deduce value type for missing template specializations recursively
我现在有一个模板,它根据作为参数给定的要存储的位数推导出具体类型:
template<unsigned char BITS>
struct Register {
using type = unsigned long;
};
template<>
struct Register<0> {
using type = void;
};
template<>
struct Register<8> {
using type = unsigned char;
};
template<>
struct Register<16> {
using type = unsigned short;
};
template<>
struct Register<32> {
using type = unsigned long;
};
是否可以扩展此模板,使其针对给定的位数自动推断出下一个更高专业化的类型?
这意味着:
- Register<1..8> 导致专业化 Register<8>
- Register<9..16>实际使用Register<16>和
- 寄存器<17..32> 导致寄存器<32>。
我想到了Register这样的解决方案,就是推导类型Register。但我不确定这是否是正确的方法,是否可以做到。
然而,对于所有 >=33 的值,它应该推断类型为 void 或 Register<0>。
示例:
Register<4>::type value; // Register<8> will be used
Register<33>::type value = 1; // Compile error -> type is void
这样可行吗?
首先,你需要引入一个定义递归的基本模板:
template< int N > struct impl_Int
{
using type = impl_Int<N+1>::type;
};
接下来,定义固定尺寸的特化:
const int impl_CHAR = sizeof(signed char) != sizeof(short)
? sizeof(signed char) * CHAR_BIT
: INT_MIN;
template<> struct Register<impl_CHAR>
{
using type = char;
};
const int impl_SHORT = sizeof(short) != sizeof(int)
? sizeof(short) * CHAR_BIT
: INT_MIN + 1;
template<> struct Register<impl_SHORT>
{
using type = short;
};
const int impl_INT = sizeof(int) != sizeof(long)
? sizeof(int) * CHAR_BIT
: INT_MIN + 2;
template<> struct Register<impl_INT>
{
using type = int;
};
const int impl_LONG = sizeof(long) * CHAR_BIT;
template<> struct Register<impl_LONG>
{
using type = long;
};
最后,定义终止特化:
template<> struct Register<INT_MAX>
{
using type = void;
};
有关实际示例,请参阅 here。
对于生产实施,您可能会将这些常量值隐藏在 impl
命名空间中,然后写入:
template <int N>
using Register = impl::Register<N>;
另请注意,您可以通过使用 C++11 fixed width integers 并对其大小进行硬编码来稍微简化此操作。这将消除对常量的需要:
#include <climits>
#include <cstdint>
template< int N > struct Register
{
using type = typename Register<N+1>::type;
};
template<> struct Register<8>
{
using type = std::int8_t;
};
template<> struct Register<16>
{
using type = std::int16_t;
};
template<> struct Register<32>
{
using type = std::int32_t;
};
template<> struct Register<64>
{
using type = void;
};
int main() {
using T1 = Register<3>::type;
using T2 = Register<35>::type;
T1 v1;
T2 v2; // This line fails to compile: T2 is void.
}
我现在有一个模板,它根据作为参数给定的要存储的位数推导出具体类型:
template<unsigned char BITS>
struct Register {
using type = unsigned long;
};
template<>
struct Register<0> {
using type = void;
};
template<>
struct Register<8> {
using type = unsigned char;
};
template<>
struct Register<16> {
using type = unsigned short;
};
template<>
struct Register<32> {
using type = unsigned long;
};
是否可以扩展此模板,使其针对给定的位数自动推断出下一个更高专业化的类型? 这意味着:
- Register<1..8> 导致专业化 Register<8>
- Register<9..16>实际使用Register<16>和
- 寄存器<17..32> 导致寄存器<32>。
我想到了Register这样的解决方案,就是推导类型Register。但我不确定这是否是正确的方法,是否可以做到。
然而,对于所有 >=33 的值,它应该推断类型为 void 或 Register<0>。
示例:
Register<4>::type value; // Register<8> will be used
Register<33>::type value = 1; // Compile error -> type is void
这样可行吗?
首先,你需要引入一个定义递归的基本模板:
template< int N > struct impl_Int
{
using type = impl_Int<N+1>::type;
};
接下来,定义固定尺寸的特化:
const int impl_CHAR = sizeof(signed char) != sizeof(short)
? sizeof(signed char) * CHAR_BIT
: INT_MIN;
template<> struct Register<impl_CHAR>
{
using type = char;
};
const int impl_SHORT = sizeof(short) != sizeof(int)
? sizeof(short) * CHAR_BIT
: INT_MIN + 1;
template<> struct Register<impl_SHORT>
{
using type = short;
};
const int impl_INT = sizeof(int) != sizeof(long)
? sizeof(int) * CHAR_BIT
: INT_MIN + 2;
template<> struct Register<impl_INT>
{
using type = int;
};
const int impl_LONG = sizeof(long) * CHAR_BIT;
template<> struct Register<impl_LONG>
{
using type = long;
};
最后,定义终止特化:
template<> struct Register<INT_MAX>
{
using type = void;
};
有关实际示例,请参阅 here。
对于生产实施,您可能会将这些常量值隐藏在 impl
命名空间中,然后写入:
template <int N>
using Register = impl::Register<N>;
另请注意,您可以通过使用 C++11 fixed width integers 并对其大小进行硬编码来稍微简化此操作。这将消除对常量的需要:
#include <climits>
#include <cstdint>
template< int N > struct Register
{
using type = typename Register<N+1>::type;
};
template<> struct Register<8>
{
using type = std::int8_t;
};
template<> struct Register<16>
{
using type = std::int16_t;
};
template<> struct Register<32>
{
using type = std::int32_t;
};
template<> struct Register<64>
{
using type = void;
};
int main() {
using T1 = Register<3>::type;
using T2 = Register<35>::type;
T1 v1;
T2 v2; // This line fails to compile: T2 is void.
}