< type_traits >头文件中提供了C++ STL的std :: is_nothrow_constructible模板。 C++ STL的std :: is_nothrow_constructible模板用于检查给定类型T是否是带有参数集的可构造类型,并且众所周知,它不会引发任何异常。如果T是可构造类型,则返回布尔值true,否则返回false。
头文件:
#include
模板类别:
template< class T, class... Args >
struct is_nothrow_constructible;
句法:
std::is_nothrow_constructible::value
参数:模板std :: is_nothrow_constructible接受以下参数:
- T:代表一种数据类型。
- Args:代表数据类型T的列表。
返回值:模板std :: is_nothrow_constructible返回一个布尔变量,如下所示:
- 是:如果类型T是可从Args构造的。
- False:如果T类型无法从Args构造。
下面是演示C++中std :: is_nothrow_constructible的程序:
程序1:
// C++ program to illustrate
// std::is_nothrow_constructible
#include
#include
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::value
<< endl;
cout << "X() is is_nothrow_constructible? "
<< is_nothrow_constructible::value
<< endl;
cout << "Y(X) is is_nothrow_constructible? "
<< is_nothrow_constructible::value
<< endl;
cout << "Z() is is_nothrow_constructible? "
<< is_nothrow_constructible::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
#include
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::value
<< '\n';
cout << "GfG is Nothrow-constructible from int and float? "
<< is_nothrow_constructible::value
<< '\n';
cout << "GfG is Nothrow-constructible from struct X? "
<< is_nothrow_constructible::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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。