【发布时间】:2021-05-12 21:27:20
【问题描述】:
我正在尝试解决将我的构建系统操作系统和 gcc 版本升级到 9 时出现的错误。我可以使用以下代码进行演示。
class Cl {
private:
float f;
public:
constexpr float GetF() const { return f; }
Cl& operator=(const Cl& other) {
f = other.GetF();
return *this;
}
Cl& operator=(const float& other) {
this->f = other;
return *this;
}
explicit constexpr Cl(const float& val) : f(val) {}
};
struct Sl {
float x, y;
Cl lcl;
constexpr Sl(const float &init_x, const float &init_y, const Cl &cl) : x(init_x), y(init_y), lcl(cl) {}
};
typedef struct Sl sdata;
int main()
{
const float fx = 30.30;
Cl c1(fx);
sdata s1(0, 0.0, c1);
return 0;
}
编译:
preetam@preetam-Precision-M4800 ~ $ g++-9 -Werror=deprecated-copy dp_test.cc
dp_test.cc: In constructor ‘constexpr Sl::Sl(const float&, const float&, const Cl&)’:
dp_test.cc:22:101: error: implicitly-declared ‘constexpr Cl::Cl(const Cl&)’ is deprecated [-Werror=deprecated-copy]
22 | constexpr Sl(const float &init_x, const float &init_y, const Cl &cl) : x(init_x), y(init_y), lcl(cl) {}
| ^
dp_test.cc:8:6: note: because ‘Cl’ has user-provided ‘Cl& Cl::operator=(const Cl&)’
8 | Cl& operator=(const Cl& other) {
| ^~~~~~~~
cc1plus: some warnings being treated as errors`
错误是什么意思?
我尝试按照此处的建议将以下内容添加到 Cl:
constexpr Cl(const Cl& other) {
f = other.GetF();
}
导致错误:
preetam@preetam-Precision-M4800 ~ $ g++-9 -Werror=deprecated-copy dp_test.cc
dp_test.cc: In copy constructor ‘constexpr Cl::Cl(const Cl&)’:
dp_test.cc:10:9: error: member ‘Cl::f’ must be initialized by mem-initializer in ‘constexpr’ constructor
10 | }
| ^
dp_test.cc:5:9: note: declared here
5 | float f;
| ^
终于用下面的拷贝构造函数修复了:
constexpr Cl(const Cl& other) : f(other.f) {}
【问题讨论】:
标签: c++ c++11 gcc g++ deprecation-warning