C++ 具名要求-基本概念-指定该类型对象可以默认构造

指定该类型对象可以默认构造

要求

以下情况下,类型 T 满足可默认构造 (DefaultConstructible)

给定

  • 任意标识符 u

下列表达式必须合法且拥有其指定的效果

表达式 后条件
T u 对象 u 被默认初始化。
T u{} 对象 u 被值初始化或聚合初始化。
T() T{} 一个 T 类型的临时对象被值初始化或聚合初始化。

注解

对于非聚合类类型的对象,必须定义公开的默认构造函数(用户定义或隐式定义),以满足可默认构造 (DefaultConstructible)

非类对象类型的非 const 对象始终可默认构造 (DefaultConstructible)

const 的非类类型并非可默认构造 (DefaultConstructible)

const 的聚合类型,若其任何成员是非类类型对象,则并非可默认构造 (DefaultConstructible)

非对象类型(函数类型,引用类型,以及(可能 cv 限定的)void 类型)和 const 的非对象类型始终并非可默认构造 (DefaultConstructible)

调用示例

复制代码
#include <iostream>
#include <type_traits>

//编译器生成默认构造函数
struct A
{
};

struct B
{
    std::string str; // 成员拥有非平凡默认构造函数
};

struct C
{
    std::string str; // 成员拥有非平凡默认构造函数
    C() throw (int) //构造函数抛异常
    {
    }
};

struct MyClass
{
    int ma;
    int mb;
    MyClass(): ma(101), mb(102)
    {
        std::cout << this << "  " << __FUNCTION__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
    }

    MyClass(int a, int b): ma(a), mb(b)
    {
        std::cout << this << "  " << __FUNCTION__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
    }
};

int main()
{
    std::cout << std::boolalpha;

    std::cout << "std::is_default_constructible<int>::value: "
              << std::is_default_constructible<int>::value << std::endl;
    std::cout << "std::is_trivially_default_constructible<int>::value: "
              << std::is_trivially_default_constructible<int>::value << std::endl;
    std::cout << "std::is_nothrow_default_constructible<int>::value: "
              << std::is_nothrow_default_constructible<int>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_default_constructible<A>::value: "
              << std::is_default_constructible<A>::value << std::endl;
    std::cout << "std::is_trivially_default_constructible<A>::value: "
              << std::is_trivially_default_constructible<A>::value << std::endl;
    std::cout << "std::is_nothrow_default_constructible<A>::value: "
              << std::is_nothrow_default_constructible<A>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_default_constructible<B>::value: "
              << std::is_default_constructible<B>::value << std::endl;
    std::cout << "std::is_trivially_default_constructible<B>::value: "
              << std::is_trivially_default_constructible<B>::value << std::endl;
    std::cout << "std::is_nothrow_default_constructible<B>::value: "
              << std::is_nothrow_default_constructible<B>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_default_constructible<C>::value: "
              << std::is_default_constructible<C>::value << std::endl;
    std::cout << "std::is_trivially_default_constructible<C>::value: "
              << std::is_trivially_default_constructible<C>::value << std::endl;
    std::cout << "std::is_nothrow_default_constructible<C>::value: "
              << std::is_nothrow_default_constructible<C>::value << std::endl;
    std::cout << std::endl;

    //T u 对象 u 被默认初始化。
    MyClass myClass1;

    //T u{} 对象 u 被值初始化或聚合初始化。
    MyClass myClass2{201, 202};

    //T() T{}一个 T 类型的临时对象被值初始化或聚合初始化。
    MyClass(803, 801);
    MyClass{303, 301};

    return 0;
}

输出

复制代码
std::is_default_constructible<int>::value: true
std::is_trivially_default_constructible<int>::value: true
std::is_nothrow_default_constructible<int>::value: true

std::is_default_constructible<A>::value: true
std::is_trivially_default_constructible<A>::value: true
std::is_nothrow_default_constructible<A>::value: true

std::is_default_constructible<B>::value: true
std::is_trivially_default_constructible<B>::value: false
std::is_nothrow_default_constructible<B>::value: true

std::is_default_constructible<C>::value: true
std::is_trivially_default_constructible<C>::value: false
std::is_nothrow_default_constructible<C>::value: false

0x61fe78  MyClass a:101 b:102
0x61fe70  MyClass a:201 b:202
0x61fe80  MyClass a:803 b:801
0x61fe88  MyClass a:303 b:301
相关推荐
吴_知遇24 分钟前
【华为OD机试真题】428、连续字母长度 | 机试真题+思路参考+代码解析(E卷)(C++)
开发语言·c++·华为od
LaoWaiHang40 分钟前
MFC案例:使用键盘按键放大、缩小窗口图像的实验
c++·mfc
到底怎么取名字不会重复1 小时前
Day10——LeetCode15&560
c++·算法·leetcode·哈希算法·散列表
陈大大陈2 小时前
基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
java·linux·开发语言·数据结构·c++·算法·缓存
纪元A梦2 小时前
华为OD机试真题——通过软盘拷贝文件(2025A卷:200分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
java·javascript·c++·python·华为od·go·华为od机试题
刚入坑的新人编程2 小时前
C++多态
开发语言·c++
QUST-Learn3D3 小时前
高精度并行2D圆弧拟合(C++)
开发语言·c++
明月醉窗台3 小时前
Qt 入门 6 之布局管理
c语言·开发语言·c++·qt
云小逸3 小时前
【C++】继承
开发语言·c++
努力学习的小廉4 小时前
【C++】 —— 笔试刷题day_21
开发语言·c++·算法