c++ - 试图了解 GCC 错误

标签 c++ gcc strict-aliasing

我有以下代码:

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>

template<typename Iterator>
void foo(Iterator begin, Iterator end)
{
   typedef typename std::iterator_traits<Iterator>::value_type type;
   type smallest = (*std::min_element(begin,end));
   std::cout << smallest << std::endl;
}

int main()
{
   std::list<int> l;
   l.push_back(1);   
   l.push_back(2);
   foo(l.begin(),l.end());
   return 0;
}

当我编译时如下:

g++ -pedantic -ansi -Wall -Werror -O2 -o  test test.cpp

我收到以下错误:

cc1plus: warnings being treated as errors
In function ‘int main()’:
cc1plus: error: dereferencing pointer ‘pretmp.163’ does break strict-aliasing rules
cc1plus: note: initialized from here

这个错误在 O3 身上出现,但在 O1 身上却没有。我已经使用 comeau 在线编译器、MS VC9.0 和 icc v11 编译了代码,在所有情况下代码编译都没有问题。

该代码适用于 std::vectorstd::dequestd::setchar*int* 迭代器,似乎是非常特定于 std::list 实现的东西。

我希望有人能提供一些关于这个特定错误(警告)的含义以及如何解决它的见解。

注意:GCC版本为:

gcc (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.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

最佳答案

我已经在 gcc-4.4 上复制了你的错误。这是来自 Debian“unstable”的 gcc-4.5 和 gcc-4.6 的非错误。

我遇到了一个与 gcc-4.4 相关的错误,类似于这个提交的错误:Bug 42488 – [4.4 only] spurious strict-aliasing warning .正如@michael-burr 所指出的,存在大量针对 gcc-4.4 的“严格别名规则”错误:GCC Bug List: strict aliasing rules 4.4 .

我意识到这有点不令人满意,但我已经接受这是 GCC 4.4 中的一个错误,并且能够转移到没有这个问题的较新版本。

关于c++ - 试图了解 GCC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2114378/

相关文章:

C fgets 和 getline 跳过来自标准输入的输入

c - xstrtoul() 在哪里/如何定义?

C 未定义的行为。严格的别名规则,还是不正确的对齐方式?

c++ - 警告!取消引用类型双关指针将打破严格别名规则 [-Wstrict-aliasing]

c++ - 如何在 Linux 上获取 C++ 代码块的时钟滴答? clock() 结果没有意义。

c++ - 如何在lambda中捕获函数结果?

c++ - 可变参数模板类 : Infer types based on constructor parameters

c - 函数中的局部变量重叠并破坏共享对象中定义的静态全局变量的内存空间

编写就地浮点型提升循环的正确方法?

c++ - 如何开始用 C++ 编写音乐可视化工具?