I have the following bit of code:
#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;
}
when I compile it as follows:
g++ -pedantic -ansi -Wall -Werror -O2 -o test test.cpp
I get the following error:
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
This error is observed with O3, but not with O1. I compiled the code using the goau online compiler, MS VC9.0 and icc v11, and in all cases the code compiles without problems.
The code works fine with the std::vector, std::deque, std::set, char*, int*iterators seem to be very specific to the implementation of std :: list.
I was hoping that someone could give some idea of what this particular error (warning) means and how to solve it.
Note. GCC Version:
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.
Matthieu N.
source
share