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
相关推荐
励志要当大牛的小白菜33 分钟前
ART配对软件使用
开发语言·c++·qt·算法
PAK向日葵2 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
QQ_4376643146 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
liulilittle7 小时前
C++/CLI与标准C++的语法差异(一)
开发语言·c++·.net·cli·clr·托管·原生
小狄同学呀7 小时前
VS插件报错,g++却完美编译?API调用错因分析
c++
程序员编程指南7 小时前
Qt 数据库连接池实现与管理
c语言·数据库·c++·qt·oracle
小乖兽技术8 小时前
C#与C++交互开发系列(二十四):WinForms 应用中嵌入C++ 原生窗体
c++·c#·交互
张北北.8 小时前
【深入底层】C++开发简历4+4技能描述6
java·开发语言·c++
刚入坑的新人编程9 小时前
暑期算法训练.9
数据结构·c++·算法·leetcode·面试·排序算法
破刺不会编程10 小时前
linux线程概念和控制
linux·运维·服务器·开发语言·c++