尝试为一个函数禁用elide构造函数,以下函数为我提供了GCC下的预期输出,但不是在clang下。在海湾合作委员会下,输出包括copy cstr called
按预期,但不包括铿锵声,这给出warning: unknown attribute 'optimize' ignored [-Wattributes]
可能发生什么?我应该使用anther关键字代替'optimize'吗?
#include <iostream>
class xyz {
public:
xyz() { std::cout << "cstr called\n"; }
xyz(const xyz& A) { std::cout << "copy cstr called\n"; }
xyz f() __attribute__((optimize("no-elide-constructors"))) {
std::cout << "f called\n";
xyz x;
return x;
}
};
int main() {
xyz a;
xyz b = a.f();
}