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
相关推荐
Charles Ray25 分钟前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
重生之我在20年代敲代码25 分钟前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法
CV工程师小林7 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
white__ice7 小时前
2024.9.19
c++
天玑y7 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
姜太公钓鲸2338 小时前
c++ static(详解)
开发语言·c++
菜菜想进步8 小时前
内存管理(C++版)
c语言·开发语言·c++
Joker100858 小时前
C++初阶学习——探索STL奥秘——模拟实现list类
c++
科研小白_d.s8 小时前
vscode配置c/c++环境
c语言·c++·vscode