用于捕获未分配的r值的编译器选项

Bal*_*ckk 8 c++ gcc return-value compiler-warnings

相当尴尬的是,我写了类似下面的东西(消毒过):

vector_item next()
{
    if (this->it == this->myVector.end()){
        this->it == this->myVector.begin();
    }
    return *(this->it++);
}
Run Code Online (Sandbox Code Playgroud)

明显的错误很明显.然而,它确实需要一段时间来追踪.

没有为此生成编译器警告,但是应该有吗?在这里创建了一个未使用的r值(并且不是说,来自为其副作用调用的函数的未使用的返回值),这在我看来是代码存在问题的指示器.

编译g++ -Wall -Wextra.(GCC 4.8.3)

我知道,-Wunused-result但这不适用于此.

Ric*_*ges 6

没有为此生成编译器警告,但是应该有吗?

我想不出我在标准中读到的任何需要警告的内容.

然而,clang开发团队似乎认为它保证一个:

18 : <source>:18:18: warning: equality comparison result unused [-Wunused-comparison]
        this->it == this->myVector.begin();
        ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
18 : <source>:18:18: note: use '=' to turn this equality comparison into an assignment
        this->it == this->myVector.begin();
                 ^~
                 =
1 warning generated.
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是,它是gcc中的QoI问题.可能值得提出它作为一个问题.

我很幸运,因为我们的软件是针对mac(clang),linux(gcc)和windows(msvc)编译的,因此可以及早发现标准侵权和边缘案例.

可能会想到在另一个编译器上重新编译你的代码然后再进行一次bug搜索 - 这对我很有帮助.