简体   繁体   English

如何避免弃用副本警告?

[英]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.

相关问题 Eigen LinSpaced - 不推荐使用的复制警告 - Eigen LinSpaced - deprecated-copy warning 冲突的 CLANG `virtual dtor` 和 `deprecated copy operator` 警告 - Conflicting CLANG `virtual dtor` and `deprecated copy operator` warnings 如何在GCC中弃用已弃用的函数中删除已弃用的警告? - How can I get rid of deprecated warnings in deprecated functions in GCC? 如何避免可能丢失数据的警告? - How to avoid warnings about possible loss of data? 如何保持内联但避免编译器警告? - How to keep calls inlined but avoid compiler warnings? 在使用JsonCpp时,如何使用已弃用的警告以及如何删除它们? - How works deprecated warnings and how to remove them when using JsonCpp? 如何避免有关未初始化变量的清晰警告 - How to avoid clang-tidy warnings about uninitialized variables 如何避免在C ++中不推荐将字符串常量转换为&#39;char *&#39; - How to avoid deprecated conversion from string constant to 'char*' in C++ 如何摆脱 GCC 中从字符串常量到“char*”的“已弃用转换”警告? - How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC? 如何正确组合基类和派生类的函数以避免警告? - How to properly combine functions from based and derived class to avoid warnings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM