试图了解GCC错误

5 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;
}
Run Code Online (Sandbox Code Playgroud)

当我编译它如下:

g++ -pedantic -ansi -Wall -Werror -O2 -o  test test.cpp
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

O3可以看到此错误,但O1不会出现此错误.我使用了在线编译器,MS VC9.0和icc v11编译了代码,并且在所有情况下代码编译都没有问题.

该代码工作正常std::vector,std::deque,std::set,char*,int*迭代器,似乎是非常具体的std ::清单实施的东西.

我希望有人可以提供一些有关此特定错误(警告)意味着什么以及如何解决它的信息.

注意: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.
Run Code Online (Sandbox Code Playgroud)

Lia*_*iao 0

没有关于 gnu 的 clu ...但这似乎很有用:http://code.google.com/p/v8/issues/detail ?id=413

  • 看起来更像是一种解决方法而不是解决方案...那么您的意思是 gcc 在这方面被破坏了? (2认同)