名稱空間
變體
操作

std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
is_default_constructibleis_trivially_default_constructibleis_nothrow_default_constructible
(C++11)(C++11)(C++11)
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <type_traits>
template< class T >
struct is_default_constructible;
(1) (C++11 起)
template< class T >
struct is_trivially_default_constructible;
(2) (C++11 起)
template< class T >
struct is_nothrow_default_constructible;
(3) (C++11 起)
1) 提供成員常量 value,等於 std::is_constructible<T>::value
2) 提供成員常量 value,等於 std::is_trivially_constructible<T>::value
3) 提供成員常量 value,等於 std::is_nothrow_constructible<T>::value

如果 T 不是完整型別,(可能 cv 限定的) void,或未知邊界陣列,則行為未定義。

如果上述模板的例項化直接或間接依賴於不完整型別,並且該例項化在該型別假設完成時可能產生不同的結果,則行為未定義。

如果程式為此頁上描述的任何模板新增特化,則行為未定義。

目錄

[編輯] 輔助變數模板

template< class T >

inline constexpr bool is_default_constructible_v =

    is_default_constructible<T>::value;
(C++17 起)
template< class T >

inline constexpr bool is_trivially_default_constructible_v =

    is_trivially_default_constructible<T>::value;
(C++17 起)
template< class T >

inline constexpr bool is_nothrow_default_constructible_v =

    is_nothrow_default_constructible<T>::value;
(C++17 起)

繼承自 std::integral_constant

成員常量

value
[靜態]
如果 T 可以預設構造,則為 true,否則為 false
(public static 成員常量)

成員函式

operator bool
將物件轉換為 bool,返回 value
(公開成員函式)
operator()
(C++14)
返回 value
(公開成員函式)

成員型別

型別 定義
value_type bool
型別 std::integral_constant<bool, value>

[編輯] 可能的實現

template<class T>
struct is_default_constructible : std::is_constructible<T> {};
 
template<class T>
struct is_trivially_default_constructible : std::is_trivially_constructible<T> {};
 
template<class T>
struct is_nothrow_default_constructible : std::is_nothrow_constructible<T> {};

[編輯] 注意

在許多實現中,std::is_nothrow_default_constructible 也檢查解構函式是否丟擲異常,因為它實際上是 noexcept(T())。同樣的,std::is_trivially_default_constructible 在這些實現中也要求解構函式是平凡的:GCC bug 51452LWG issue 2116

std::is_default_constructible<T> 不會測試 T x; 是否能編譯;它嘗試使用空引數列表進行直接初始化(參見 std::is_constructible)。因此,std::is_default_constructible_v<const int>std::is_default_constructible_v<const int[10]> 都是 true

[編輯] 示例

#include <string>
#include <type_traits>
 
struct S1
{
    std::string str; // member has a non-trivial default constructor
};
static_assert(std::is_default_constructible_v<S1> == true);
static_assert(std::is_trivially_default_constructible_v<S1> == false);
 
struct S2
{
    int n;
    S2() = default; // trivial and non-throwing
};
static_assert(std::is_trivially_default_constructible_v<S2> == true);
static_assert(std::is_nothrow_default_constructible_v<S2> == true);
 
int main() {}

[編輯] 參閱

檢查型別是否具有針對特定引數的建構函式
(類模板) [編輯]
檢查型別是否具有複製建構函式
(類模板) [編輯]
檢查型別是否可以從右值引用構造
(類模板) [編輯]
指定型別的物件可以預設構造
(概念) [編輯]