[英]How to avoid deprecated-copy warnings?
Here is some simplified code.这是一些简化的代码。 It's not mine, but I'm adapting it for a project of mine.
这不是我的,但我正在为我的一个项目改编它。 It's part of a sizeable codebase I'm importing, and which I don't want to change more than absolutely necessary.
它是我正在导入的一个相当大的代码库的一部分,我不想对它进行超过绝对必要的更改。
void function (object& out_thing)
{
object thing;
#pragma omp nowait
for (int k = 0 ; k < 5 ; k++) {
object* things[2];
float scores[2];
for (i = 0; i < 2 ; i++)
scores[i] = evaluateThings(things[i], parameter1, parameter2);
if (scores[1] < scores[0]) {
scores[0] = scores[1];
things[0] = things[1];
}
thing = *(things[0]);
}
out_thing = thing;
}
When compiled, I get warnings that the implicitly declared thing = *(things[0]) and out_thing = thing are deprecated [-Wdeprecated-copy] because I have a user-provided copy constructor.编译时,我收到警告,指出隐式声明的 thing = *(things[0]) 和 out_thing = thing 已弃用 [-Wdeprecated-copy],因为我有一个用户提供的复制构造函数。
I guess the compiler wants me to write object thing(*(things[1])
but I can't because I need to declare object thing
before the omp pragma, and I can't write object out_thing(thing)
at the end because out_thing is already defined as one of the arguments passed to the function.我想编译器要我写
object thing(*(things[1])
但我不能,因为我需要在 omp pragma 之前声明object thing
,而且我不能在最后写object out_thing(thing)
因为out_thing 已经定义为传递给 function 的 arguments 之一。
Is there any way to recode this to get rid of the warnings?有没有办法重新编码以消除警告?
(I can actually get rid of the first one by changing object thing;
to object* thing = NULL
and then later on changing thing = *(things[0]);
to thing = things[0];
but that requires me to change out_thing = thing;
to out_thing = *thing;
and I still get the warning for that deprecated copy; I'd like to get rid of both, ideally, if it's possible without extensive changes elsewhere in the code base and without being harmful to performance etc.) (实际上,我可以通过将
object thing;
更改为object* thing = NULL
然后再将thing = *(things[0]);
更改为thing = things[0];
来摆脱第一个,但这需要我进行更改out_thing = thing;
to out_thing = *thing;
并且我仍然收到关于该已弃用副本的警告;理想情况下,我想摆脱这两者,如果可以在代码库的其他地方不进行大量更改并且不损害性能的话ETC。)
The warning -Wdeprecated-copy
warns about a missing/deleted user-declared copy assignment operator, when there is the user-declared destructor ~object
.当存在用户声明的析构函数
~object
时,警告-Wdeprecated-copy
警告用户声明的复制赋值运算符丢失/删除。 If there is a user-declared destructor, missing a copy constructor or a copy assignment operator is likely the error in the code.如果存在用户声明的析构函数,则缺少复制构造函数或复制赋值运算符很可能是代码中的错误。 You should add a custom copy assignment operator
您应该添加自定义复制赋值运算符
object& operator=(const object&);
See What is The Rule of Three?请参阅什么是三规则? .
.
This warning is usually triggered when the compiler expects a copy operator in case of occurance of a destructor in the code.当编译器期望在代码中出现析构函数时使用复制运算符时,通常会触发此警告。 So check for such instances where you might have missed a copy assignment operator in your code.
因此,请检查您可能在代码中遗漏了复制赋值运算符的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.