<type_traits>

類模板
<type_traits>

std::is_constructible

template <class T, class... Args> struct is_constructible;
可構造性判斷

如果型別 T 可以用給定引數列表 Arg 列表進行構造,則該 trait 類表現為 integral_constant,其值為 true,否則為 false。Arg.

對於該類,可構造型別 是指可以使用特定引數集來構造的型別。

is_constructible繼承自 integral_constant,其值型別為 bool,表示 T 是否可以由引數列表 Args 構造。T即可構造的型別Args.

模板引數

T
一個完整型別,或者void(可能帶 cv 限定),或者一個未知邊界的陣列。
Args
一組完整的型別(或void,可能帶 cv 限定,或一個未知界陣列),表示建構函式形式的引數型別,順序與建構函式中的相同。
如果省略,則檢查該型別是否為預設可構造

成員型別

繼承自 integral_constant
成員型別定義
value_typebool
型別true_type 或 false_type

成員常量

繼承自 integral_constant
成員常量定義
truefalse

成員函式

繼承自 integral_constant

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// is_constructible example
#include <iostream>
#include <type_traits>

struct A { A (int,int) {}; };

int main() {
  std::cout << std::boolalpha;
  std::cout << "is_constructible:" << std::endl;
  std::cout << "int: " << std::is_constructible<int>::value << std::endl;
  std::cout << "int(float): " << std::is_constructible<int,float>::value << std::endl;
  std::cout << "int(float,float): " << std::is_constructible<int,float,float>::value << std::endl;
  std::cout << "A: " << std::is_constructible<A>::value << std::endl;
  std::cout << "A(int): " << std::is_constructible<A,int>::value << std::endl;
  std::cout << "A(int,int): " << std::is_constructible<A,int,int>::value << std::endl;
  std::cout << "A(int,int,int): " << std::is_constructible<A,int,int,int>::value << std::endl;
  return 0;
}

輸出
is_constructible:
int: true
int(float): true
int(float,float): false
A: false
A(int): false
A(int,int): true
A(int,int,int): false


另見