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
相关推荐
南郁2 小时前
007-nlohmann/json 项目应用-C++开源库108杰
c++·开源·json·nlohmann·现代c++·d2school·108杰
菠萝014 小时前
共识算法Raft系列(1)——什么是Raft?
c++·后端·算法·区块链·共识算法
海棠蚀omo4 小时前
C++笔记-C++11(一)
开发语言·c++·笔记
凌佚5 小时前
rknn优化教程(一)
c++·目标检测·性能优化
Lenyiin7 小时前
《 C++ 点滴漫谈: 四十 》文本的艺术:C++ 正则表达式的高效应用之道
c++·正则表达式·lenyiin
yxc_inspire9 小时前
基于Qt的app开发第十三天
c++·qt·app·tcp·面向对象
虾球xz9 小时前
CppCon 2015 学习:Concurrency TS Editor’s Report
开发语言·c++·学习
潇-xiao9 小时前
Qt 按钮类控件(Push Button 与 Radio Button)(1)
c++·qt
板鸭〈小号〉10 小时前
命名管道实现本地通信
开发语言·c++
YKPG11 小时前
C++学习-入门到精通【14】标准库算法
c++·学习·算法