【问题标题】:Whats the meaning of gcc/g++ error 'implicitly-declared' constructor is 'deprecated'?gcc/g++ 错误“隐式声明”构造函数被“弃用”是什么意思?
【发布时间】: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


    【解决方案1】:

    您正在为lcl(cl) 使用复制构造函数,尽管您没有定义一个,但您定义了一个用户定义的赋值运算符。您可以通过添加用户定义的构造函数来修复它:

    class Cl {
            private:
                    float f;
            public:
            constexpr float GetF() const { return f; }
            constexpr Cl(const Cl& other) : f(other.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;
    }
    

    Nitpick:“错误的含义是什么?” 这不是错误,而是警告。该项目可以编译并按预期运行,但问题当然应该得到解决。

    【讨论】:

    • 该项目无法编译,因为它的构建标志将此视为错误。无论如何,我仍然得到一个错误 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在 'constexpr' 构造函数 10 | } | ^ dp_test.cc:5:9: 注意:这里声明 5 |浮动 f; | ^
    • @preetam 无法重现:wandbox.org/permlink/wVyTEaLaGpS30tmk 你在构造函数中使用了成员初始化列表吗?
    • 复制构造函数与你在代码中提出的不同。
    • 感谢您提供演示代码。比起规范,我更能理解示例。
    【解决方案2】:

    Sl 结构的lcl 成员在Sl 的构造函数中初始化。 这种初始化意味着复制构造函数的使用。

    但是,正如编译器的消息所述,如果我们提供我们自己版本的复制赋值运算符,这肯定意味着在这样的复制操作中有一些微妙之处,那么复制构造函数应该是也明确提供。

    如果没有提供copy-assign操作符,则认为默认的合适,默认的copy-constructor也可能合适。

    请注意,您应该始终考虑rule of five or zero

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多