std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible
來自 cppreference.com
定義於標頭檔案 <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 起) |
如果 T
不是完整型別,(可能 cv 限定的) void,或未知邊界陣列,則行為未定義。
如果上述模板的例項化直接或間接依賴於不完整型別,並且該例項化在該型別假設完成時可能產生不同的結果,則行為未定義。
如果程式為此頁上描述的任何模板新增特化,則行為未定義。
目錄 |
[編輯] 輔助變數模板
template< class T > inline constexpr bool is_default_constructible_v = |
(C++17 起) | |
template< class T > inline constexpr bool is_trivially_default_constructible_v = |
(C++17 起) | |
template< class T > inline constexpr bool is_nothrow_default_constructible_v = |
(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 51452,LWG 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() {}
[編輯] 參閱
(C++11)(C++11)(C++11) |
檢查型別是否具有針對特定引數的建構函式 (類模板) |
(C++11)(C++11)(C++11) |
檢查型別是否具有複製建構函式 (類模板) |
(C++11)(C++11)(C++11) |
檢查型別是否可以從右值引用構造 (類模板) |
(C++20) |
指定型別的物件可以預設構造 (概念) |