[英]Do compilers avoid intermediate integral promotion or conversions?
Consider a class like: 考虑一个类:
struct mystruct
{
constexpr operator char() {return x;}
signed char x;
};
and an operation like: 和一个像这样的操作:
mystruct m;
m.x = /* something at runtime */
int i = 3 * m + 45ULL * m;
Are compilers able to skip the temporary conversion to char
and convert directly m
to the required type in the 3 * m + 45ULL * m
expression? 编译器是否能够跳过临时转换为char
并直接将m
转换为3 * m + 45ULL * m
表达式中所需的类型?
Seems like GCC version 5.3.0 is able to optimize the call to cast function, while Clang 3.7 is not as smart. 似乎GCC版本5.3.0能够优化对强制转换功能的调用,而Clang 3.7则不如智能。
For this piece of code: 对于这段代码:
struct mystruct
{
constexpr operator char() const {return x;}
signed char x;
} m;
void func(const int num) {
m.x = num*2;
int i = 3 * m + 45ULL * m;
}
you can check compiled assembly and compare them: 你可以检查编译的程序集并比较它们:
Clang with cast vs Clang with direct reference to field Clang与演员 vs Clang直接引用场
Gcc with cast vs Gcc with direct reference to field Gcc与演员 vs Gcc直接引用场
Though in a slightly different situation Clang does manage to optimize the call to cast function. 虽然在一个稍微不同的情况下,Clang确实设法优化对强制转换函数的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.