是否可以保证size_t是整数类型之一的别名类型? [英] Is size_t guaranteed to be an alias type to one of integer types?

查看:52
本文介绍了是否可以保证size_t是整数类型之一的别名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者它可以是单独的无符号整数类型吗?

对于不同的(无符号)整数类型,我对模板函数有不同的专长.我需要为 size_t 提供单独的专业化吗?

解决方案

C ++标准说:

18.2/2内容与标准C库头相同,但有以下更改:

18.2/6类型size_t是实现定义的无符号整数类型,其大小足以容纳任何对象的字节大小.

18.2/7 [注意:建议实现为ptrdiff_t和size_t选择其整数类型转换等级(4.13)不得大于有符号long int的等级,除非需要更大的尺寸才能包含所有可能的值.—尾注]

因此,没有明确说明实现定义的无符号整数类型是否为 unsigned short int long long long .18.2/6存在并指定实现定义的无符号整数类型"这一事实可能会覆盖18.2/2的默认值(紧随其后的C),因此C的任何答案都不能被C ++信任.

建议重新转换等级表示 size_t 将是4.13中提到的类型之一,其中未明确提及 size_t ,但明显的候选对象是,但不能保证.

我需要为size_t提供单独的专业化吗?

size_t 是一种独特的类型时,您可以使用 std :: is_same std :: enable_if 来执行此操作./p>

Or can it be a separate unsigned integer type?

I have different specializations of a template function for different (unsigned) integer types. Do I need to provide a separate specialization for size_t?

解决方案

The C++ Standard says:

18.2/2 The contents are the same as the Standard C library header , with the following changes:

18.2/6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.

18.2/7 [ Note: It is recommended that implementations choose types for ptrdiff_t and size_t whose integer conversion ranks (4.13) are no greater than that of signed long int unless a larger size is necessary to contain all the possible values. —end note ]

So, it doesn't say explicitly whether the implementation-defined unsigned integer type will be one of unsigned short, int, long, long long. The fact that 18.2/6 exists and specifies an "implementation-defined unsigned integer type" may be seen to override 18.2/2's default of following C, so any answer for C can't be trusted for C++.

The recommendation re conversion ranks implies the size_t will be expected to be one of the types mentioned in 4.13, where size_t isn't explicitly mentioned but the obvious candidates are, but that's no guarantee.

Do I need to provide a separate specialization for size_t?

You could use std::is_same and std::enable_if to do so when size_t is a distinct type....

这篇关于是否可以保证size_t是整数类型之一的别名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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