【发布时间】:2017-05-03 02:22:37
【问题描述】:
我怀疑 gcc 4.8.3 是否内联了不正确的模板函数...这个问题在调试模式下不会出现,而只会在优化模式下出现。但是,这发生在复杂的代码库中,我无法在简单的测试用例中重现该问题。
我的代码如下
#include "stdio.h"
class A {
public:
template<typename T> int WriteNative(const T) {
printf("here?\n")
return 0;
}
template<typename D>
void doit() {
if (WriteNative<double>(1)) {
printf("A\n");
} else {
printf("B\n");
}
}
};
// in my real code, this definition is in a different cpp file
template<> int A::WriteNative<double>(const double) {
return 1;
}
int main() {
A a;
a.doit<float>();
}
在调试版本中,它会打印出 A,而在优化版本中,它会在此处打印出来?\nB
我猜任何内联器都使用通用模板函数定义,但不是专用的。但是 attribute ((noinline)) 没有帮助。
如果我的代码具有已定义的 C++ 行为,有人吗?以及如何解决这个问题?
【问题讨论】:
-
我找到的解决方法是内联专门的定义:stackoverflow.com/questions/4445654/…。
标签: c++ templates gcc inline compiler-optimization