这是什么意思? int foo = foo + 4; [英] What does this mean? int foo = foo + 4;

查看:171
本文介绍了这是什么意思? int foo = foo + 4;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

int main(int argc, char** args) {
  int foo = foo + 4;
  std::cout << foo << std::endl;
}

后续问题是,有一个编译器标志停止这种事情?我发现 - 有时工作,更好地是完全防止它。

And a follow up question, is there a compiler flag to stop this sort of thing? I found -Wall works sometimes, better would be to prevent it entirely.

My compiler:  
g++ -v  
Using built-in specs.  
Target: i486-linux-gnu  
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu  
Thread model: posix  
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)  


推荐答案

没有没有可靠的方法来阻止这种事情。它是C ++和C的一个组成部分,变量的名称在其初始化器中可见。它允许你做这样的事情

No there is no guaranteed way to stop this sort of thing. It is an integral part of C++ and C that the variable's name is visible in its initializer. It allows you to do such things as

T *t = malloc(sizeof(*t));

有关C ++问题列表的问题报告,需要在简单的情况下进行诊断,但目前编译器不需要诊断你的情况。

There is an issue report on the C++ issues list to require diagnostics in simple cases, but currently compilers are not required to diagnose your case.

它在不同的上下文中也有效 < a>。

It's also valid in a different context.

编辑:澄清 - 您的代码段的行为未定义:您正在读取未初始化变量的值。编译器不需要诊断这不意味着行为被定义。

To clarify - the behavior of your snippet is undefined: You are reading the value of a not initialized variable. That compilers are not required to diagnose this does not mean that behavior is defined.

这篇关于这是什么意思? int foo = foo + 4;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆