为什么要使用“const”?这里需要:VS C2664
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(那些
LPCSTR
/LPSTR
类型名只会混淆代码。)您在这里遇到的问题可以用以下简洁的方式表达
此代码无法编译的原因与您的相同原始版本无法编译:这种引用初始化在 C++ 中是非法的。在您的示例中,在函数参数初始化中隐式使用相同的初始化(当从
func2
调用func1
时),而在我的示例中,它是显式完成的。它非法的原因与
T**
几乎相同 ->const T**
转换在 C++(和 C)中是非法的。这是一个旧的常见问题解答:http://www.parashift .com/c++-faq-lite/const- Correctness.html#faq-18.17。基本上,如果上面示例中的初始化是合法的,则可以继续执行以下操作序列,
这意味着它将允许我们在没有任何“黑客”的情况下打破 const 正确性规则,即不使用单次强制转换。这在 C++(以及 C)中被认为是不可接受的,这就是像
T **
-> 这样的转换的原因。const T **
和诸如T *&
-> 之类的初始化不允许const T *&
。另请注意,就像
T**
->const T* const*
转换在 C++ 中是合法的(请参阅常见问题解答条目),T**
->const T* const&
初始化也是合法的(Those
LPCSTR
/LPSTR
typenames only obfuscate the code. )The issue you have here can be expressed in the following succinct way
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
fromfunc2
), 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
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 likeT *&
->const T *&
are not allowed.Note also, that just like
T**
->const T* const*
conversion is legal in C++ (see the FAQ entry), theT**
->const T* const&
initialization is legal as well在该代码示例中,将生成 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.
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.