Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c++ - Is "const LPVOID" equivalent to "void * const"?

And if so, why some Win32 headers use it?

For instance:

BOOL APIENTRY VerQueryValueA( const LPVOID pBlock,
    LPSTR lpSubBlock,
    LPVOID * lplpBuffer,
    PUINT puLen
    );

A bit more elaboration: If the API never uses references (or any other C++-only constructs) but only pointers and values, what is the point of having const LPVOID vs. LPCVOID.

Should I treat every place I see const LPVOID as some place where the real meaning is LPCVOID? (and thus it is safe to add a cast)

Further clarification: It appears that const LPVOID pBlock was indeed a mistake in this case. Windows 2008 SDK replaces it to LPCVOID in VerQueryValue signature. Wine did so quite some time ago.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

A typedef-name denotes a type, and not a sequence of tokens (as does a macro). In your case, LPVOID denotes the type also denoted by the token sequence void *. So the diagram looks like

// [...] is the type entity, which we cannot express directly.
LPVOID => [void *] 

Semantically if you specify the type const LPVOID, you get the following diagram (the brackets around the specifiers mean "the type denoted by the specifier"):

// equivalent (think of "const [int]" and "[int] const"):
const LPVOID <=> LPVOID const =>  const [void *] <=> [void *] const  
                              =>  ["const qualified void-pointer"]

It's not the same thing as the token sequence const void * - because this one would not denote a const qualified pointer type, but rather a pointer to a const qualified type (the thing pointed to would be const).

Syntactically a parameter declaration has the following (simplified) form:

declaration-specifiers declarator

The declaration-specifiers in case of const void *p are const void - so the base-type of *p is a const qualified void, but the pointer itself is not qualified. In case of const LPVOID p however the declaration-specifiers specify a const qualified LPVOID - which means the pointer type itself is qualified, making the parameter declaration identical to void *const p.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...