<type_traits>頭文件中提供了C++ STL的std::is_nothrow_constructible模板。 C++ STL的std::is_nothrow_constructible模板用於檢查給定類型T是否是帶有參數集的可構造類型,並且眾所周知,它不會引發任何異常。如果T為可構造類型,則返回布爾值true,否則返回false。
頭文件:
#include<type_traits>
模板類別:
template< class T, class... Args > struct is_nothrow_constructible;
用法:
std::is_nothrow_constructible<T, Args..>::value
參數:模板std::is_nothrow_constructible接受以下參數:
- T:它代表一種數據類型。
- Args:它表示數據類型T的列表。
返回值:模板std::is_nothrow_constructible返回一個布爾變量,如下所示:
- True:如果類型T是可從Args構造的。
- False:如果T類型無法從Args構造。
下麵是演示C++中std::is_nothrow_constructible的程序:
程序1:
// C++ program to illustrate
// std::is_nothrow_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
// Declare structures
struct X {
};
struct Y {
Y() {}
Y(X&)
noexcept {}
};
struct Z {
int n;
Z() = default;
};
// Driver Code
int main()
{
cout << boolalpha;
cout << "int is_nothrow_constructible? "
<< is_nothrow_constructible<int>::value
<< endl;
cout << "X() is is_nothrow_constructible? "
<< is_nothrow_constructible<X>::value
<< endl;
cout << "Y(X) is is_nothrow_constructible? "
<< is_nothrow_constructible<Y, X>::value
<< endl;
cout << "Z() is is_nothrow_constructible? "
<< is_nothrow_constructible<Z>::value
<< endl;
return 0;
}
輸出:
int is_nothrow_constructible? true X() is is_nothrow_constructible? true Y(X) is is_nothrow_constructible? false Z() is is_nothrow_constructible? true
程序2:
// C++ program to illustrate
// std::is_nothrow_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
// Class GfG
class GfG {
int v1;
float v2;
public:
GfG(int n)
:v1(n), v2()
{
}
GfG(int n, double f) noexcept:v1(n), v2(f) {}
};
// Declare Structure
struct X {
int n;
X() = default;
};
// Driver Code
int main()
{
cout << boolalpha;
cout << "GfG is Nothrow-constructible from int? "
<< is_nothrow_constructible<GfG, int>::value
<< '\n';
cout << "GfG is Nothrow-constructible from int and float? "
<< is_nothrow_constructible<GfG, int, float>::value
<< '\n';
cout << "GfG is Nothrow-constructible from struct X? "
<< is_nothrow_constructible<GfG, X>::value
<< '\n';
}
輸出:
GfG is Nothrow-constructible from int? false GfG is Nothrow-constructible from int and float? true GfG is Nothrow-constructible from struct X? false
參考:http://www.cplusplus.com/reference/type_traits/is_nothrow_constructible/
相關用法
- C語言 strtok()、strtok_r()用法及代碼示例
- C語言 memset()用法及代碼示例
- C++ std::mismatch()用法及代碼示例
- C++ wcscpy()用法及代碼示例
- C++ wcscmp()用法及代碼示例
- C++ set_symmetric_difference用法及代碼示例
- C++ ratio_equal()用法及代碼示例
- C++ std::equal_to用法及代碼示例
- C++ quick_exit()用法及代碼示例
- C++ multiset lower_bound()用法及代碼示例
- C++ multiset upper_bound()用法及代碼示例
- C++ multiset max_size()用法及代碼示例
- C++ forward_list max_size()用法及代碼示例
- C++ std::allocator()用法及代碼示例
- C++ array data()用法及代碼示例
- C++ multiset size()用法及代碼示例
- C++ ratio_not_equal()用法及代碼示例
注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 std::is_nothrow_constructible in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。