本文介绍了是“const LPVOID”相当于“void * const”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是,为什么有些Win32头使用它?

And if so, why some Win32 headers use it?

例如:

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

更详细的说明:如果API从不使用引用(或任何其他C ++ - 只有指针和值,有什么点有 const LPVOID LPCVOID

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.

我应该把每个地方看待 const LPVOID 作为一个真正意义 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)

进一步说明:看起来 const LPVOID pBlock 这种情况下的错误。 Windows 2008 SDK将它替换为 VerQueryValue 签名中的 LPCVOID

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.

推荐答案

typedef名称表示一个类型,而不是一系列令牌宏)。在你的情况下, LPVOID 表示也由令牌序列 void * 表示的类型。所以图表看起来像

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 *]

语义 c> const LPVOID ,您将得到以下图表(括号中的说明符表示由说明符表示的类型):

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"]

这不是作为令牌序列 const void * - 因为这不会表示const限定的指针类型,而是一个指向const限定类型的指针(指向的东西将是const )。

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

声明说明符 const void * p const void - 所以基本类型 * p 是一个const限定 void ,但指针本身不合格。然而,在 const LPVOID p 的情况下,声明说明符指定一个const限定 LPVOID - 这意味着指针类型本身是合格,使参数声明等同于 void * const p

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.

这篇关于是“const LPVOID”相当于“void * const”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:06