为什么要使用“const”?这里需要:VS C2664

发布于 2024-11-02 19:01:29 字数 405 浏览 1 评论 0原文

我从 MSDN 复制

代码“在 Visual C++ 2005 中,编译器现在强制执行应用 const 的 C++ 标准要求。以下示例生成 C2664。”

// C2664d.cpp
// C2664 expected
#include <windows.h>

void func1(LPCSTR &s)
{

}

void func2(LPSTR &s)
{
   func1(s);
}

int main()
{
   return 0;
}

为什么我需要在这里使用“const”?

I copy code from MSDN

It says that "In Visual C++ 2005, the compiler now enforces the C++ standard requirements for applying const. The following sample generates C2664."

// C2664d.cpp
// C2664 expected
#include <windows.h>

void func1(LPCSTR &s)
{

}

void func2(LPSTR &s)
{
   func1(s);
}

int main()
{
   return 0;
}

Why do I need to use "const" here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

大海や 2024-11-09 19:01:29

(那些 LPCSTR/LPSTR 类型名只会混淆代码。)

您在这里遇到的问题可以用以下简洁的方式表达

char *p = NULL;
const char *&r = p; // ERROR

此代码无法编译的原因与您的相同原始版本无法编译:这种引用初始化在 C++ 中是非法的。在您的示例中,在函数参数初始化中隐式使用相同的初始化(当从 func2 调用 func1 时),而在我的示例中,它是显式完成的。

它非法的原因与 T** 几乎相同 -> const T** 转换在 C++(和 C)中是非法的。这是一个旧的常见问题解答:http://www.parashift .com/c++-faq-lite/const- Correctness.html#faq-18.17

基本上,如果上面示例中的初始化是合法的,则可以继续执行以下操作序列,

const char cc = 0;
r = &cc; // OK. Note: `p` now points to `cc`!
*p = 1; // !!! Attempts to modify `cc` !!!

这意味着它将允许我们在没有任何“黑客”的情况下打破 const 正确性规则,即不使用单次强制转换。这在 C++(以及 C)中被认为是不可接受的,这就是像 T ** -> 这样的转换的原因。 const T ** 和诸如 T *& -> 之类的初始化不允许 const T *&

另请注意,就像 T** -> const T* const* 转换在 C++ 中是合法的(请参阅常见问题解答条目),T** -> const T* const& 初始化也是合法的

char *p = 0;
const char *const &r = p; // OK

(Those LPCSTR/LPSTR typenames only obfuscate the code. )

The issue you have here can be expressed in the following succinct way

char *p = NULL;
const char *&r = p; // ERROR

This code does not compile for the very same reason your original version does not compile: such reference initialization is illegal in C++. In your example the same initialization is used implicitly in function parameter initialization (when calling func1 from func2), while in my example it is done explicitly.

The reason it is illegal is pretty much the same the T** -> const T** conversion is illegal in C++ (and in C). This is an old FAQ: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17.

Basically, if the initialization in my above example were legal, one would be able to continue with the following sequence of operations

const char cc = 0;
r = &cc; // OK. Note: `p` now points to `cc`!
*p = 1; // !!! Attempts to modify `cc` !!!

meaning that it would allow us to break the const-correctness rules without any "hacks", i.e. without using a single cast. This is considered unacceptable in C++ (as well as in C), which is the reason conversions like T ** -> const T ** and initializations like T *& -> const T *& are not allowed.

Note also, that just like T** -> const T* const* conversion is legal in C++ (see the FAQ entry), the T** -> const T* const& initialization is legal as well

char *p = 0;
const char *const &r = p; // OK
鯉魚旗 2024-11-09 19:01:29

在该代码示例中,将生成 C2664,因为代码尝试将对指针的引用转换为对 const 指针的引用。这是两个不同的事情。

允许转换为对指针的 const 引用 (const LPSTR&)。

In that code sample, C2664 will be generated because the code is attempting to convert a reference to a pointer into a reference to a const pointer. Which are two different things.

Converting to a const reference to a pointer (const LPSTR&) would be allowed.

聽兲甴掵 2024-11-09 19:01:29

LPCSTR 定义为“typedef CONST CHAR *LPCST”并且
LPSTR 定义为“typedef CHAR *LPCST”

如您所见,LPCSTR 是 const,而 LPSTR 不是,因此这两种类型不同,因此您在上下文中需要 const。

LPCSTR is defined as "typedef CONST CHAR *LPCST" and
LPSTR is defined as "typedef CHAR *LPCST"

As you can see, LPCSTR is const and LPSTR is not, so the two types are different and hence you need const in your context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文