Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

c++ - Purpose of uppercase VOID macro & INT typedef in winnt.h

Anyone any clue as to why there is an uppercase VOID macro defined in the winnt.h header?

To make matters more confusing, VOID is a macro, whereas CHAR, SHORT, INT, and LONG are typedefs.

See the relevant excerpt from winnt.h:

#ifndef VOID
#define VOID void
typedef char CHAR;
typedef short SHORT;
typedef long LONG;
#if !defined(MIDL_PASS)
typedef int INT;
#endif
#endif

A historical reason perhaps for doing VOID* pointer instead of void* pointer?

EDIT: What is even more troubling is to see people using VOID instead of void when doing Windows programming today. You can also see it as part of MSDN docs, e.g. http://msdn.microsoft.com/en-us/library/bb205867(v=vs.85).aspx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Windows API is old. Really old. Older than the official C standard old.

This means that, at the start, the Windows API had to deal with all kinds of ancient C compilers, with different levels of language support. Some might not support void. Some might have an int type that's not compatible with what windows thought was int. Some might not understand short. As a workaround, the Windows API provides upper-case portable equivalents that are aliased to whatever works for that particular compiler.

Of course, with modern compilers, things have settled down quite a bit. Everyone supports void, for example. However, in order to maintain compatibility with old code that uses these upper-case macros, the #defines and typedefs have to remain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...