Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么是INTA;a=std::max(a,x)不';"t emit";“未初始化”;警告_C++_Compiler Warnings_Static Analysis - Fatal编程技术网

C++ 为什么是INTA;a=std::max(a,x)不';"t emit";“未初始化”;警告

C++ 为什么是INTA;a=std::max(a,x)不';"t emit";“未初始化”;警告,c++,compiler-warnings,static-analysis,C++,Compiler Warnings,Static Analysis,考虑以下代码: #include <vector> #include <algorithm> #include <iostream> int main() { std::vector<int> v{{1, 2, 3}}; int a; std::cout << a << std::endl; // 1 for (const int x : v) { a = std:

考虑以下代码:

#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> v{{1, 2, 3}};
    int a;

    std::cout << a << std::endl;    // 1

    for (const int x : v) {
        a = std::max(a, x);         // 2
    }

    std::cout << a << std::endl;

    return 0;
}
正如您所看到的,CLang和solstudio只能检测案例(1)和忽略案例(2),而g++同时忽略这两种情况。在病例(2)中是否有并发症?为什么g++在这方面如此糟糕

我使用的编译器选项:

$ g++-5 -std=c++11 -Wall -Wpedantic -pedantic -Wextra \
         -Wuninitialized -Wmaybe-uninitialized aisa.cpp
$ clang++ -std=c++11 -Wall -Wpedantic -pedantic -Wextra -Wuninitialized aisa.cpp
$ CC -std=c++11 -xprevise aisa.cpp

std::max
通过
const&
获取其参数,而流式操作符
首先:两个编译器只诊断第一次攻击,即它们只报告第一次未初始化使用
a
。因此,为了得到第二条警告,我们需要删除第一行:

#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> v{{1, 2, 3}};
    int a;

    for (const int x : v) {
        a = std::max(a, x);         // 2
    }

    std::cout << a << std::endl;

    return 0;

}
#包括
#包括
#包括
int main(){
std::向量v{{1,2,3};
INTA;
用于(常量整数x:v){
a=std::max(a,x);//2
}

STD:CUT考虑在GCC和CLAN的bug跟踪器上提交一个特征请求-像这样的琐碎的情况可以用附加的逻辑来检测如果移除第一个示例会发生什么?那么Color或Solaris Studio会警告变量化的变量吗?如果你优化了优化变量,你也可以得到更好的警告。l、 @MartinBonner:g++-5使用
-O2
检测(1),删除大小写(1)无助于使(2)可捕获(事实上,我在注意到编译器未能检测(2)后添加了大小写(1))如果它涉及引用,则不发出警告确实是有意义的。但这是一个常量引用。我认为在这种情况下,编译器实际上可以发出警告,因为无法初始化常量引用传递的对象。@MarTijn如果函数的唯一目的是将
const*
转换为对象并将其用作映射键?您可以从
常量(&
)执行此操作,甚至可以从未初始化的对象执行此操作。@MarTijn:如果函数保存对象的地址,则稍后的操作可以初始化变量。(或者函数可以将地址单独用作某种唯一标记).Valid points!TBH我从不使用这样的结构,因此编译器选项可能更可取。您的观点似乎有效,但
intmymax(inta,intb){return(a>b)?a:b;}
帮助不大,案例(2)仍然没有涵盖“初犯”意思是第一次出现特定的警告,对吗?不是一般的警告?@QPaysTaxes是的。如果你能更清楚地说明,请随意编辑。是的,我使用了g++5。谢谢你的回答!
#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> v{{1, 2, 3}};
    int a;

    for (const int x : v) {
        a = std::max(a, x);         // 2
    }

    std::cout << a << std::endl;

    return 0;

}