开发者

basic.types §3.9 /9th point,ISO Standard C++ specification

开发者 https://www.devze.com 2023-02-06 14:59 出处:网络
Point from ISO Standard C++ specification:§3.9 /9th point An object type is a (possibly cv qualifi开发者_StackOverflowed) type that is not a function type, not a reference type, and not a void type

Point from ISO Standard C++ specification : §3.9 /9th point

An object type is a (possibly cv qualifi开发者_StackOverflowed) type that is not a function type, not a reference type, and not a void type.

Any one can able to explain this point with programming .(how it fails )


Any one can able to explain this point with programming .(how it fails )

Bending over backwards, I came up with the following program:

std::cout << sizeof(bool(int)) << std::endl;
// invalid application of 'sizeof' to a function type

std::cout << sizeof(void) << std::endl;
// invalid application of 'sizeof' to a void type

That is because:

The sizeof operator yields the number of bytes in the object representation of its operand.

I cannot apply the same reasoning for references because:

When applied to a reference or a reference type, the result is the size of the referenced type.

But we can use the fact that arrays are sequences of objects:

int& array[10];
// error: declaration of 'array' as array of references

This is an error because references are not objects. Happy now? ;)


Are you making it harder than you have to?

There are various "things" in C++, such as functions, references, pointers, classes, and so forth. Almost everything has a type (e.g. variables) or is a type (e.g. classes), and some of those types are said to be "object types". Function types, reference types, and void are not "object types".

Note that void is a special type with several special cases around it. In particular, it's used to mean "nothing" in function return types, and to mean "unknown type" when used as void* (a "pointer to an unknown type").

"Reference types" should be fairly self-explanatory: add a & to an existing type to get a reference type. (You can't do this with all types.)

Function types is also exactly as it sounds: the type of a function. For example:

void f();
int h(double);

Here, f has type void() and h has type int(double). (Function types look weird compared to most other types.)

0

精彩评论

暂无评论...
验证码 换一张
取 消