<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++ 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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。