I've got a very strange g++ warning when tried to compile following code:
#include <map>
#include <set>
class A {
public:
int x;
int y;
A(): x(0), y(0) {}
A(int xx, int yy): x(xx), y(yy) {}
bool operator< (const A &a) const {
return (x < a.x || (!(a.x < x) && y < a.y));
}
};
struct B {
std::set<A> data;
};
int
main()
{
std::map<int, B> m;
B b;
b.data.insert(A(1, 1));
b.data.insert(A(1, 2));
b.data.insert(A(2, 1));
m[1] = b;
return 0;
}
Output:
$ g++ -Wall -W -O3 t.cpp -o /tmp/t
t.cpp: In function ‘int main()’:
t.cpp:14: warning: dereferencing pointer ‘__x.52’ does break strict-aliasing rules
t.cpp:14: warning: dereferencing pointer ‘__x.52’ does break strict-aliasing rules
/usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/bits/stl_tree.h:525: note: initialized from here
It doesn't have any sence to me at all. How should I interpret it ? I don't see what's wrong with the code posted.
Forget to specify compiler details:
$ gcc --version
gcc (GCC) 4.4.2 20091027 (Red Hat 4.4.2-7)
gcc 4.4 has a bug where std::map breaks incorrectly warns about strict-aliasing rules.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39390
Your code is valid C++. Strict aliasing merely allows a subset of optimizations that are enabled by default when using -O3.
Your solution is to compile with -fno-strict-aliasing or a different version of gcc.
If you're curious about what strict aliasing is, that has been asked here.
Try to change this:
return (x < a.x || (!(a.x < x) && y < a.y));
into:
return (x < a.x || (a.x == x && y < a.y));
I also compiled this using your version and my version under g++ 3.4.2 and is all ok.
Which version of g++? g++ 4.3.2 compiles this without complaint.
Related
I'm trying to update a c++ project's compiler from 4.8.4 to 7.5.0.
The codes can be well compiled with g++4.8.4. However, when I try to compile it with newer g++ 7.5.0. Lots of Werror=conversion errors are triggered.
Here is the demo code abstracted from my project:
// main.cc
template <typename T>
int Hello(T obj) {
float arg = 1.0;
// return obj.func(arg); // good, error detected for both g++ 4.8.4 and 7.5.0
return obj.func(arg) * 2; // failed to detect conversion error for 4.8.4, success for 7.5.0
}
class A {
public:
int func(int a) {
return a;
}
};
int main() {
A a;
Hello(a);
return 0;
}
build:
g++ main.cc -Werror=conversion
As commented above, in a c++ template function, g++4.8 failed to detect the type conversion error when it's in a expression. But it will sucess if it is alone.
I'm very curious about this.
My Question:
Is this a bug of g++4.8? Why does expression matter?
When i tried to compile with icpc it shows "expression must have class type." Got confused about this. Please help.
int main()
{
__m256d temp;
temp.m256d_f64[0] = 1;
return 0;
}
I can reproduce this problem on my end. In the immintrin.h shipped with Intel Compiler, we have the following definition for __m256d:
typedef struct _MMINTRIN_TYPE(32) __m256d {
double m256d_f64[4];
} __m256d;
In the above definition, the struct name and alias are same which is confusing the current compiler. Intel Compiler doesn't seem to recognize the typedef name as a class which can proved with a smaller testcase:
$ cat test1.cc
typedef struct __m256d {
double m256d_f64[4];
} m256d;
int main()
{
__m256d temp;
temp.m256d_f64[0] = 1;
return 0;
}
$ icpc test1.cc –c
When I change the typedef and instantiate temp as shown below (use the typedefed name instead of struct name), ICC fails but GCC works:
$ cat test1.cc
typedef struct m256d {
double m256d_f64[4];
} __m256d;
int main()
{
__m256d temp;
temp.m256d_f64[0] = 1;
return 0;
}
$ icpc test1.cc -c
test1.cc(8): error: expression must have class type
temp.m256d_f64[0] = 1;
^
compilation aborted for test1.cc (code 2)
$ g++ test1.cc -c
I have reported this issue to compiler engineering team at Intel.
I have the following code:
typedef int __v8si __attribute__ ((__vector_size__ (32)));
class T
{
public:
__v8si v;
__attribute__((target("avx2")))
T(int p ):v(__extension__ __v8si{p,p,p,p,p,p,p,p}){}
__attribute__((target("avx2")))
T(__v8si p):v(p){}
__attribute__((target("avx2")))
T operator * ( T b ) const
{
return __builtin_ia32_pmulld256( v , b.v ) ;
}
};
void h(int *);
__attribute__((target("default")))
void h(int *)
{
}
__attribute__((target("avx2")))
void h(int *)
{
const T a(1);
const T b(2);
const T c (a * b);
}
int main()
{
return 0;
}
If I try to compile it,
I have the following error:
$ gcc test.cpp -std=c++11
test.cpp: In member function 'T T::operator*(T) const':
test.cpp:17:48: internal compiler error: in convert_move, at expr.c:315
return __builtin_ia32_pmulld256( v , b.v ) ;
^
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
I have tried to compile it with various gcc versions:
4.9.2, 5.3 and several other versions.
Have you any ideas why it happens, and how could I solve the problem?
gcc test.cpp -std=c++11 -mavx2 works.
gcc test.cpp -std=c++11 -O1 works.
but I want to compile the code with -O0
Update 1. I've filed a bug report. The report is available here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77952
I've reduced a case to the code shown below here.
When compiling, this gives the following:
$ g++ -std=c++0x -O2 -Wall t.cpp
t.cpp: In function ‘int main()’:
t.cpp:20: warning: dereferencing pointer ‘<anonymous>’ does break strict-aliasing rules
t.cpp:19: warning: dereferencing pointer ‘<anonymous>’ does break strict-aliasing rules
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:175: note: initialized from here
What is this warning telling me ? What can I do about it ?
#include <stdio.h>
#include <stdint.h>
struct TimeKey {
uint64_t time_stamp;
uint64_t msg_no;
TimeKey(uint64_t tstamp, uint64_t no) :
time_stamp(tstamp),
msg_no(no)
{}
bool operator < (const TimeKey &other) const
{
if (time_stamp == other.time_stamp) //line 19
return msg_no < other.msg_no; //line 20
else
return time_stamp < other.time_stamp;
}
};
template <typename T>
class TimeBuffer {
public:
uint64_t counter;
std::map<TimeKey, T> messages;
void AddMsg(uint64_t tstamp, T val) {
messages[TimeKey(tstamp, counter++)] = val;
}
};
int main(void)
{
TimeBuffer<int> messages;
messages.AddMsg(123456, 1);
}
Note, this is on RHEL 6.3, which comes with gcc 4.4.6
This is a known (and fixed) compiler bug, see this and this. You should update your toolchain.
I have this code
#include <vector>
#include <array>
template <typename T>
struct Vertice
{
T elements_[4];
Vertice(const T & x, const T & y, const T & z)
{
elements_[0] = x;
elements_[1] = y;
elements_[2] = z;
elements_[3] = T(1);
}
Vertice() : Vertice(T(0), T(0), T(0)) {}
};
typedef Vertice<float> VerticeF;
std::array<VerticeF, 5> v2;
and returns following error when compiling with gcc 4.5.2:
$ g++ -o tutorial tutorial.cpp -std=gnu++0x
tutorial.cpp: In constructor ‘Vertice<T>::Vertice() [with T = float]’:
/usr/include/c++/4.5/tr1_impl/array:50:5: instantiated from here
tutorial.cpp:28:41: error: type ‘Vertice<float>’ is not a direct base of ‘Vertice<float>
However, if I don't use constructor delegation, works properly.
Why?
GCC 4.5 does not support constructor delegation; you need to use GCC 4.7; see http://gcc.gnu.org/projects/cxx0x.html.