【问题标题】:Constructor requirements in C++11 for POD [duplicate]POD 的 C++11 中的构造函数要求 [重复]
【发布时间】:2012-08-27 01:26:08
【问题描述】:

可能重复:
What are Aggregates and PODs and how/why are they special?

C++11 中的 struct 需要什么样的构造函数才能将这个 struct 保持为 POD?

只接受初始化列表?或者可能没有任何限制?

【问题讨论】:

    标签: c++ c++11


    【解决方案1】:

    你需要一个默认的默认构造函数,这样它就很简单了:

    struct pot
    {
        constexpr pot() noexcept = default;
    
        pot(int a, float b) : x(a), y(b) { }
    
        int x;
        float y;
    };
    

    constexprnoexcept 是可选的,但我们也可以。

    用法:

    pot p;         // OK
    pot q(1, 1.5); // also OK
    

    【讨论】:

    • 为什么在这里使用 constexpr 作为构造函数?
    • @NikitaTrophimov:您不妨提供最有力的保证,不是吗?这样你就可以在常量表达式中使用类型。
    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2015-02-02
    • 2014-09-03
    • 1970-01-01
    • 2018-01-23
    • 2012-05-11
    相关资源
    最近更新 更多