【发布时间】:2014-04-12 08:15:46
【问题描述】:
如果我写
template<int sign>
inline int add_sign(int x) {
return sign * x;
}
template int add_sign<-1>(int x);
template int add_sign<1>(int x);
大多数 C++ 编译器是否足够聪明,可以将乘法 1 或 -1 优化为更快的运算(无运算或取反)?
【问题讨论】:
-
为什么不检查编译器的汇编输出?很可能会回答您的问题。
-
我不知道怎么做。我该怎么做?
-
第一步是搜索“[你的编译器]生成程序集”
-
定义“现代”。第一个 Fortran 编译器是在 1957 年这样做的。
标签: c++ templates compiler-construction compiler-optimization sign