基于最大可能数字确定编译时的索引类型 [英] Determining index type at compile time based on largest possible number
问题描述
我试图创建一个 index_type_< N>
类型,将返回基于索引的类型
N
其中 N
是索引可以转到的最大数字。
$例如:
index_type_< 32> :: type index1; // uint8_t
index_type_< 64000> :: type index2; // uint16_t
index_type_< 18446744073709551> :: type index3; // uint64_t
我有以下代码拒绝编译,我无法确定原因,在错误消息中搜索Google,但这些都不符合我的情况。
#include< iostream>
template< size_t N,typename T = void>
struct index_type_;
template< size_t N>
struct index_type_< N,typename std :: enable_if< N< = 256,uint8_t> :: value>
{
typedef uint8_t type;
};
template< size_t N,typename T = void>
struct index_type_;
template< size_t N>
struct index_type_< N,typename std :: enable_if< N< = 65536,uint16_t> :: value>
{
typedef uint16_t type;
};
template< size_t N>
struct index_type_< N,typename std :: enable_if< N< = 4294967296,uint32_t> :: value>
{
typedef uint32_t type;
};
template< size_t N>
struct index_type_< N,typename std :: enable_if< N< = 18446744073709551616ULL,uint64_t> :: value>
{
typedef uint64_t类型;
};
int main()
{
index_type_< 32> :: type index1;
index_type_< 232122> :: type index2;
index_type_< 992532523> :: type index3;
index_type_< 4213662352328854> :: type index4;
std :: cout<< index1:<< sizeof(index1)< std :: endl;
std :: cout<< index2:<< sizeof(index2)< std :: endl;
std :: cout<< index3:<< sizeof(index3)< std :: endl;
std :: cout<< index4:<< sizeof(index4)< std :: endl;
}
错误和示例代码可以在这里找到:
$ b
任何帮助都会非常感激,所有您的专业化是模糊的。例如。哪一个是以下的最佳选择?
模板< std :: size_t N&
struct Foo {
//专业化1
};
template< std :: size_t N>
struct Foo< N> {
//专业化2
};
int main(){
Foo< 1> foo; //错误:部分专门化'Foo< N>'不
//专门化任何模板参数。
}
尝试这样:
模板< std :: uintmax_t N>
struct index_type {
using type = std :: conditional_t< N< = 255,std :: uint8_t,
std :: conditional_t< = 63535,std :: uint16_t,
std :: conditional_t< =< = 4294967295,std :: uint32_t,
std :: conditional_t< N< = 18446744073709551615ULL,std :: uint64_t,
std :: uintmax_t> ;>> ;;
};
template< std :: uintmax_t N>
using index_type_t = typename index_type< N> :: type;
用法:
code> index_type_t< 64000>测试; // unsigned int
I'm attempting to make an index_type_<N>
type that will return the type
of the index based on N
where N
is the maximum number to the indices can go to.
So for example:
index_type_<32>::type index1; //uint8_t
index_type_<64000>::type index2; //uint16_t
index_type_<18446744073709551>::type index3; //uint64_t
I have the following code that refuses to compile and I can't determine the cause despite searching for Google on the error messages, none of them seem relevant to my situation.
#include <iostream>
template<size_t N, typename T = void>
struct index_type_;
template<size_t N>
struct index_type_<N, typename std::enable_if<N <= 256, uint8_t>::value>
{
typedef uint8_t type;
};
template<size_t N, typename T = void>
struct index_type_;
template<size_t N>
struct index_type_<N, typename std::enable_if<N <= 65536, uint16_t>::value>
{
typedef uint16_t type;
};
template<size_t N>
struct index_type_<N, typename std::enable_if<N <= 4294967296, uint32_t>::value>
{
typedef uint32_t type;
};
template<size_t N>
struct index_type_<N, typename std::enable_if<N <= 18446744073709551616ULL, uint64_t>::value>
{
typedef uint64_t type;
};
int main()
{
index_type_<32>::type index1;
index_type_<232122>::type index2;
index_type_<992532523>::type index3;
index_type_<4213662352328854>::type index4;
std::cout << "index1:" << sizeof(index1) << std::endl;
std::cout << "index2:" << sizeof(index2) << std::endl;
std::cout << "index3:" << sizeof(index3) << std::endl;
std::cout << "index4:" << sizeof(index4) << std::endl;
}
The errors and the sample code can be found here:
Any help would be greatly appreciated, I feel I'm missing something obvious.
All your specializations are ambiguous. E.g. which one is the best choice of the following?
template <std::size_t N>
struct Foo {
// Specialization 1
};
template <std::size_t N>
struct Foo<N> {
// Specialization 2
};
int main() {
Foo<1> foo; // Error: partial specialization 'Foo<N>' does not
// specialize any template arguments.
}
Try something like this instead:
template <std::uintmax_t N>
struct index_type {
using type = std::conditional_t<N <= 255, std::uint8_t,
std::conditional_t<N <= 63535, std::uint16_t,
std::conditional_t<N <= 4294967295, std::uint32_t,
std::conditional_t<N <= 18446744073709551615ULL, std::uint64_t,
std::uintmax_t>>>>;
};
template <std::uintmax_t N>
using index_type_t = typename index_type<N>::type;
Usage:
index_type_t<64000> test; // unsigned int
这篇关于基于最大可能数字确定编译时的索引类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!