相关疑难解决方法(0)

用于启用详尽且详细的g ++警告的标志

通常在C下gcc,我将从以下一组警告标志开始(从多个来源痛苦地组装):

-Wall -Wextra -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast \
-Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef \
-Wnested-externs -Wcast-qual -Wshadow -Wwrite-strings -Wno-unused-parameter \
-Wfloat-equal -pedantic -ansi
Run Code Online (Sandbox Code Playgroud)

我将使用这组警告构建(至少我的调试版本)并修复我可能做的所有事情(通常是一切),然后只删除标记,如果它们不相关或不可修复(几乎从不这样).有时,-Werror如果我必须在编译时离开,我也会添加.

我只是拿起C++(是的,我落后了15年),我想从右脚开始.

我的问题是:是否有人为C++预先编译了类似的完整警告标志集g++?(我知道其中很多都是一样的.)

c++ warnings g++

113
推荐指数
3
解决办法
4万
查看次数

转换构造函数与转换运算符:优先级

在这里阅读关于转换运算符和构造函数的一些问题让我思考它们之间的相互作用,即当存在"模糊"调用时.请考虑以下代码:

class A;

class B { 
      public: 
         B(){} 

         B(const A&) //conversion constructor
         { 
              cout << "called B's conversion constructor" << endl; 
         } 
};

class A { 
      public: 
         operator B() //conversion operator
         { 
              cout << "called A's conversion operator" << endl; 
              return B(); 
         } 
};

int main()
{
    B b = A(); //what should be called here? apparently, A::operator B()
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码显示"被称为A的转换运算符",这意味着调用转换运算符而不是构造函数.如果operator B()从中删除/注释掉代码A,编译器将很乐意切换到使用构造函数(不对代码进行其他更改).

我的问题是:

  1. 由于编译器不认为B b = A();是一个模糊的调用,因此这里必须有某种类型的优先级.这个优先权究竟在哪里确定?(将赞赏C++标准的参考/引用)
  2. 从面向对象的哲学角度来看,这是代码应该表现的方式吗?谁更了解A对象应该如何成为一个B对象, …

c++ constructor operators type-conversion conversion-operator

66
推荐指数
1
解决办法
1万
查看次数

你最喜欢的g ++选项是什么?

我是C++编程的新手.编译时我从不使用任何选项.

这是我的日常命令:

g++ MyCode.cc -o MyCode
Run Code Online (Sandbox Code Playgroud)

对于安全实践,最好的选择是什么?

c++ unix compiler-construction gcc g++

26
推荐指数
8
解决办法
1万
查看次数

在GCC中使用-O3有什么缺点吗?

我已经在各种语言中担任了13年的软件工程师,尽管我现在正在进入C和后来的C++.当我正在学习C时,我正在使用GCC编译器来编译我的程序,我想知道是否有使用-O3或其他优化标志的问题.如果没有测试编译的代码,或者在交叉编译期间,我的软件可能会破坏我无法捕获的方式,我可能会无意中混淆了某个不同的平台.

在我盲目地打开这些选项之前,我想知道我能期待什么.此外,由于-Ofast打开了不符合标准的标志,我倾向于不使用它.在我的假设中,我是否正确--Ofast很可能会产生"副作用?"

在发布此问题之前,我已经浏览了https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html.

c c++ gcc

9
推荐指数
3
解决办法
1367
查看次数

我应该使用哪些编译标志来避免运行时错误

刚刚在这里学到,-Wsequence-point当代码可以调用UB时,comiplation标志会弹出警告.我在类似的声明上尝试过

int x = 1;
int y = x+ ++x;
Run Code Online (Sandbox Code Playgroud)

它工作得非常好.到目前为止,我已编译gccg++仅使用-ansi -pedantic -Wall.你有没有其他有用的标志来使代码更安全和健壮?

c c++ compiler-flags compiler-warnings sequence-points

8
推荐指数
1
解决办法
353
查看次数

在C++ 11中将char*转换为bool时强制执行类型安全性

以下代码编译正常,没有任何警告(使用g ++的默认选项).在这种情况下,是否有一个标志可以用来让g ++发出警告?

void foo(bool v) {
}

void bar() {
  foo("test");
}
Run Code Online (Sandbox Code Playgroud)

c++ gcc gcc-warning implicit-conversion c++11

6
推荐指数
1
解决办法
527
查看次数

为什么将double转换为看似任何原语的const引用?

考虑以下代码:

#include <iostream>

float func(char const & val1, unsigned int const & val2)
{
    return val1 + val2;
}

int main() {
    double test1 = 0.2;
    double test2 = 0.3;

    std::cout << func(test1, test2) << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

尽管我将a传递double给采用const引用小于a的类型的函数double(在我的系统上sizeof(double) == 8,while sizeof(unsigned int) == 4sizeof(char) == 1按定义),但该函数仍可编译和运行。如果引用不是const,则编译失败(例如, float func(char & val1, unsigned int & val2)而不是当前定义),并显示以下错误:

不能将'char&'类型的非常量左值引用绑定到'char'类型的右值

在Godbolt上使用GCC,Clang,ICC和MSVC进行测试时,我得到的行为完全相同,因此它看起来是标准的。导致该接受的const-references是什么呢,而引用不是?另外,我曾经使用过-Wall -pedantic-为什么我没有收到有关转换范围缩小的警告?当函数按值传递而不是按引用传递时...

c++ language-lawyer reference-binding

4
推荐指数
1
解决办法
225
查看次数

为什么"gcc -Wall"没有警告"if(ptr <0)"?

(长话故事......你可以直接跳到最后的问题......)

我需要使用realpath(3)所以我写了一个简单的例子来尝试它:

$> cat realpath.c
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    char pathname[PATH_MAX];

    if (realpath(argv[1], pathname) < 0) {
        perror("realpath");
        return 1;
    }

    printf("%s\n", pathname);

    return 0;
}
$> gcc -Wall -o realpath realpath.c
$> ls /xxx
ls: cannot access '/xxx': No such file or directory
$> ./realpath /xxx/foo/bar
/xxx
Run Code Online (Sandbox Code Playgroud)

结果./realpath /xxx/foo/bar让我感到惊讶.根据手册,失败更有意义ENOENT.我甚至提到了POSIX并没有找到答案.经过一段时间后,我重读了手册并找到了realpath(3)退货char *而不是int.我真的很恼火gcc.

那么为什么 gcc(甚至没有 …

c gcc pointers gcc-warning

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

更好的编译器警告?

是否可以向g ++添加其他警告标志,以便它在下面的代码中警告我关于单元化的b变量?

#include <iostream>
using namespace std;
int main() {
  int a, b;
  cin >> a;
  while (a>0) b++;
  cout << a;
}
Run Code Online (Sandbox Code Playgroud)

编辑:我忘了提到我已经尝试打开另一个问题中列出的标志: 标志启用彻底和冗长的g ++警告但没有触发.("嘀嗒!",如下所述.)

c++ g++ compiler-warnings

1
推荐指数
1
解决办法
277
查看次数