相关疑难解决方法(0)

为什么关于未初始化变量的g ++警告取决于变量的类型?(它警告一个int但不是一个双)

我目前正在尝试理解g ++在哪些情况下警告未初始化的变量.考虑以下代码:

#include <iostream>

typedef double barType;

struct foo {

    barType bar;

};

int main() {

    foo f;

    std::cout << f.bar << std::endl;

}
Run Code Online (Sandbox Code Playgroud)

如果我像这样编译它我没有得到警告:

$ g++ -O1 -Wall test.cc -o test
Run Code Online (Sandbox Code Playgroud)

但如果我将barType更改为int:

$ g++ -O1 -Wall test.cc -o test
  test.cc: In function ‘int main()’:
  test.cc:17: warning: ‘f.foo::bar’ is used uninitialized in this function
Run Code Online (Sandbox Code Playgroud)

警告如何取决于类型?在这两种情况下都没有初始化.

我正在使用:

$ g++ --version
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. …
Run Code Online (Sandbox Code Playgroud)

c++ g++

12
推荐指数
2
解决办法
1400
查看次数

标签 统计

c++ ×1

g++ ×1