整数类型的最大值? [英] Max value of an integer type?

查看:68
本文介绍了整数类型的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个模板,我需要在编译时知道给定的

整数类型的最大值。类似于:


模板< class NumType>

class Arb {

public:


static NumType const max = / *最大值* /;


};

如果模板只用于无符号整数类型,然后

我会做以下事情:


模板< class NumType>

class Arb {

public:


静态NumType const max = -1;


};

我需要一个编译时常量,其值为

整数类型的最大值。


任何想法?


如果溢出一个有符号整数没有未定义的行为,我可以使用元编程技术,如下所示:


template< class T, bool overflow = false>

struct MaxIntVal {

private:


static T const internal = 1 + MaxIntVal< T,。 ..


公开:


s tatic T const val = SCHAR_MAX +内部;

};

-


Frederick Gotham

解决方案

* Frederick Gotham:


我正在写一个模板,我需要知道最大值在编译时给定的

整数类型。



template< typename T struct Max;

template< struct Max< int> {static int const value = INT_MAX; };

等等


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么并在电子邮件中?


Frederick Gotham写道:


>

我需要一个编译时常量来计算

整数类型的最大值。



你确定它必须是编译时间?如果不只是使用

std :: numeric_limits< T> :: max()

-

Ian Collins。




我有一个解决方案(虽然我打算让它更具编译时间 -

,因为我可以假设数字至少为7。


#include< limits>


模板< T级,

T shift_by = 0,

bool no_more_digits = shift_by == std :: numeric_limits

< T> :: digits


>



struct IntMax {

private:


static T const one = 1;
静态T const val =(1< IntMax< T,shift_by +

one> :: val;


};


template< class T ,T shift_by>

struct IntMax< T,shift_by,true {


static T const val = 0;

};

#include< iostream>

int main()

{

std :: cout<< ;


"最大值\ n"

" ============ \ n \ n"


"签名字母: << (int)IntMax< signed char> :: val<<


" \ n签名短:" << IntMax< short> :: val<<


" \ n Signed int:" << IntMax< int> :: val<<


" \ n签名长:" << IntMax< long> :: val;


}

-


Frederick Gotham


I''m writing a template, and I need to know the maximum value of a given
integer type at compile time. something like:

template<class NumType>
class Arb {
public:

static NumType const max = /* maximum value */;

};
If the template were only to be used with unsigned integer types, then
I''d do the following:

template<class NumType>
class Arb {
public:

static NumType const max = -1;

};
I need a compile-time constant which evaluates to the maximum value of an
integer type.

Any ideas?

If it weren''t undefined behaviour to overflow a signed integer, I could
use a metaprogramming technique such as the following:

template<class T, bool overflow = false>
struct MaxIntVal {
private:

static T const internal = 1 + MaxIntVal<T,...

public:

static T const val = SCHAR_MAX + internal;
};
--

Frederick Gotham

解决方案

* Frederick Gotham:

I''m writing a template, and I need to know the maximum value of a given
integer type at compile time.

template< typename T struct Max;
template<struct Max<int>{ static int const value = INT_MAX; };
and so on

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Frederick Gotham wrote:

>
I need a compile-time constant which evaluates to the maximum value of an
integer type.

Are you sure it has to be compile time? If not just use
std::numeric_limits<T>::max()
--
Ian Collins.



I have a solution (although I intend on making it more compile-time-
efficient as I can assume that "digits" is at least 7).

#include<limits>

template< class T,
T shift_by = 0,
bool no_more_digits = shift_by == std::numeric_limits
<T>::digits

>

struct IntMax {
private:

static T const one = 1;

public:

static T const val = (one << shift_by) | IntMax<T,shift_by +
one>::val;

};

template<class T, T shift_by>
struct IntMax<T,shift_by,true{

static T const val = 0;
};
#include <iostream>
int main()
{
std::cout <<

"Max values\n"
"============\n\n"

" Signed char: " << (int)IntMax<signed char>::val <<

"\nSigned short: " << IntMax<short>::val <<

"\n Signed int: " << IntMax<int>::val <<

"\n Signed long: " << IntMax<long>::val;

}
--

Frederick Gotham


这篇关于整数类型的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆