當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ std::is_constructible模板用法及代碼示例

<type_traits>頭文件中提供了C++ STL的std::is_constructible模板。 C++ STL的std::is_constructible模板用於檢查給定類型T是否是帶有參數集的可構造類型。如果T為可構造類型type,則返回布爾值true,否則返回false。

頭文件:

#include<type_traits>

模板類別:

template <class T, class... Args>
struct is_constructible;

用法:

std::is_constructible::value

參數:



  • T:它代表數據類型。
  • Args:它表示數據類型T的列表。

返回值:該模板返回一個布爾變量,如下所示:

  • 正確:如果類型T是可構造的。
  • False:如果類型T是不可構造的。

下麵是說明C /C++中std::is_constructible模板的程序:

程序:

// C++ program to illustrate 
// std::is_constructible example 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Declare structures 
struct A { 
}; 
  
struct T { 
    T(int, int){}; 
}; 
  
// Driver Code 
int main() 
{ 
    cout << std::boolalpha; 
  
    // Check if <int> is 
    // constructible or not 
    cout << "int:"
         << is_constructible<int>::value 
         << endl; 
  
    // Check if <int, float> is 
    // constructible or not 
    cout << "int(float):"
         << is_constructible<int, float>::value 
         << endl; 
  
    // Check if <int, float, float> is 
    // constructible or not 
    cout << "int(float, float):"
         << is_constructible<int, float, float>::value 
         << endl; 
  
    // Check if struct T is 
    // constructible or not 
    cout << "T:"
         << is_constructible<T>::value 
         << endl; 
  
    // Check if struct <T, int> is 
    // constructible or not 
    cout << "T(int):"
         << is_constructible<T, int>::value 
         << endl; 
  
    // Check if struct <T, int, int> is 
    // constructible or not 
    cout << "T(int, int):"
         << is_constructible<T, int, int>::value 
         << endl; 
    return 0; 
}
輸出:
int:true
int(float):true
int(float, float):false
T:false
T(int):false
T(int, int):true

參考: http://www.cplusplus.com/reference/type_traits/is_constructible/




相關用法


注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 std::is_constructible template in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。