gpt4 book ai didi

c++ - 模板替换在调试构建中失败,但在优化构建中工作?

转载 作者:太空狗 更新时间:2023-10-29 23:06:30 26 4
gpt4 key购买 nike

<分区>

我遇到一个奇怪的问题,使用 -Ox 编译的代码可以正常编译,但是使用 -g 编译的代码在链接时失败并出现以下错误:

Undefined symbols for architecture x86_64:
"stupid<dummy>::t", referenced from:
stupid<dummy>::operator()(int, int) const in main.o

重现此问题的代码:

struct dummy { void operator()(const int a, const int b) const {} ; };

template <typename T>
struct stupid {
constexpr static T t = T(); // create a new one;

stupid() {};
void operator()(int a, int b) const { t(a, b); }
};


int main()
{
const stupid<dummy> a;
a( 1, 2 );
}

貌似代码优化时,函数是内联的,不需要外部函数调用,但是代码未优化时,调用了函数却没有? (我不确定为什么它不存在......)。这发生在 g++ 4.7 和 4.8 以及 clang 3.2 中。

有什么想法吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com