问题描述
这是我在使用Valgrind的服务器应用程序中发现的错误.
This was a bug I found in a server application using Valgrind.
struct Foo
{
Foo(const std::string& a)
: a_(a_)
{
}
const std::string& a_;
};
使用gcc -Wall,您不会收到警告.为什么这样的法律法规?
with gcc -Wall you don't get a warning.Why is this legal code?
推荐答案
所违反的内容 8.3.2/4 A ...引用应初始化为引用有效的对象或函数
.因此,这绝对是非法的.
What you've got violates 8.3.2/4 A ... reference shall be initialized to refer to a valid object or function
. So it is most certainly illegal.
请注意,并非所有错误程序都必须由编译器检测,尽管老实说我认为这是其中之一.
Note that not all erroneous programs are required to be detected by the compiler, although I honestly would have thought this was one of them.
值得一提的是,启用最大编译器警告的g ++版本4.4.1可以愉快地接受该程序,而不会发出警告:
For what it's worth, g++ version 4.4.1 with maximal compiler warnings turned on happily accepts this program without a warning either:
int main(void)
{
int *p = 0;
*p = 5;
}
这篇关于初始化具有合法性的参考会员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!