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
相关推荐
※DX3906※14 分钟前
cpp实战项目—string类的模拟实现
开发语言·c++
萌の鱼1 小时前
leetcode 2080. 区间内查询数字的频率
数据结构·c++·算法·leetcode
xianwu5432 小时前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql
敖行客 Allthinker4 小时前
从 UTC 日期时间字符串获取 Unix 时间戳:C 和 C++ 中的挑战与解决方案
linux·运维·服务器·c++
IU宝4 小时前
list的使用,及部分功能的模拟实现(C++)
开发语言·c++
青草地溪水旁5 小时前
c++ list的front和pop_front的概念和使用案例
c++·容器·list
扎量丙不要犟6 小时前
跨平台的客户端gui到底是选“原生”还是web
前端·javascript·c++·qt·rust·electron·tauri
linhhanpy6 小时前
自制虚拟机(C/C++)(二、分析引导扇区,虚拟机读二进制文件img软盘)
c语言·汇编·c++·python·stm32·操作系统
Stanford_11067 小时前
C++中常用的十大排序方法之4——希尔排序
c++·算法·微信小程序·排序算法·微信公众平台·twitter·微信开放平台
青草地溪水旁8 小时前
c++ list的front和pop_front的概念和使用案例—第2版
开发语言·c++·list