Can compilers avoid intermediate integration or conversion?

Consider a class like:

struct mystruct { constexpr operator char() {return x;} signed char x; }; 

and operation like:

 mystruct m; mx = /* something at runtime */ int i = 3 * m + 45ULL * m; 

Can compilers skip the temporary conversion to char and convert directly m to the required type in the expression 3 * m + 45ULL * m ?

+5
source share
1 answer

It seems that GCC version 5.3.0 can optimize the translation function call, while Clang 3.7 is not so smart.

For this piece of code:

 struct mystruct { constexpr operator char() const {return x;} signed char x; } m; void func(const int num) { mx = num*2; int i = 3 * m + 45ULL * m; } 

you can check the compiled assembly and compare them:

Clang with cast vs Clang with a direct link to the field

Gcc with cast vs Gcc with direct link to the field

Although in a slightly different situation, Clang does manage to optimize the translation function call.

0
source

Source: https://habr.com/ru/post/1238612/


All Articles