所有基本整数类型上的重载是否足以捕获所有整数? [英] Is overloading on all of the fundamental integer types is sufficient to capture all integers?

查看:85
本文介绍了所有基本整数类型上的重载是否足以捕获所有整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我对所有标准整数类型都有函数重载:

Let's say I have function overloads for all standard integer types:

void foo( char );
void foo( signed char );
void foo( short );
void foo( int );
void foo( long );
void foo( long long );
// .... all unsigned variants as well

这些重载是否可能无法为int8_t之类的类型找到合适的重载?是否有一种便携式方法来处理此类超载?

Is it possible that those overloads would fail to find proper overload for types like int8_t or something like that? Is there a portable way to handle such overloads?

参考如何?

为澄清这个问题:它来自对这个问题的讨论

To clarify the question: it comes from discussion of this question Why is int8_t read as a character? and claims that there could be compiler generated integer types that would be not an alias to fundamental C++ types. So in such case overloads for all fundamental case may not accept it. On another way I cannot provide overload for int8_t as on many platforms it is just an alias and will get error for redefinition of existing overload.

推荐答案

该标准不能保证标准整数类型是编译器唯一支持的整数类型.实际上,C和C ++标准明确地允许编译器定义其他整数类型,这些类型统称为扩展整数类型":

The standard does not provide a guarantee that the standard integer types are the only integer types supported by the compiler. Indeed, the C and C++ standards explicitly give permission to compilers to define other integer types, collectively known as "extended integer types":

可能还有实现定义的扩展有符号整数类型.

同样,对于每种扩展的有符号整数类型,都有一个对应的扩展的无符号整数类型 ...

Likewise, for each of the extended signed integer types there exists a corresponding extended unsigned integer type ...

扩展的有符号整数类型和扩展的无符号整数类型统称为扩展的整数类型.

the extended signed integer types and extended unsigned integer types are collectively called the extended integer types.

并且C标准不禁止实现使用扩展整数类型来实现stdint.h类型.为了清楚起见,甚至还有这种非规范性的符号:

And the C standard does not prohibit an implementation from using extended integer types to implement the stdint.h types. There is even this non-normative notation, just to make it clear:

其中一些类型可能表示实现定义的扩展整数类型.

Some of these types may denote implementation-defined extended integer types.

如果要使用可以采用任何整数类型的函数,则有两个选择:将其设为模板(可能使用SFINAE禁止使用非整数类型)或提供采用std::intmax_t的单个重载.该标准要求intmax_t是支持的最大整数:

If you want a function that can take any integer type, you have two options: make it a template (probably using SFINAE to disallow non-integral types) or provide a single overload that takes std::intmax_t. The standard requires that intmax_t is the largest integer supported:

表示一个有符号整数类型,该类型可以表示任何值 任何带符号的整数类型

designates a signed integer type capable of representing any value of any signed integer type

当然,有符号整数类型"包括标准类型和扩展类型.因此std::intmax_t可以处理实现支持的任何带符号整数.

And of course "signed integer type" includes standard and extended types. So std::intmax_t can handle any signed integer that the implementation supports.

这篇关于所有基本整数类型上的重载是否足以捕获所有整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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