<type_traits>

類模板
<type_traits>

std::is_convertible

template <class From, class To> struct is_convertible;
可轉換為

用於識別隱式轉換為.

該類繼承自 integral_constant,根據T支援賦值U.


模板引數

從, 到
完整型別,或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
// is_convertible example
#include <iostream>
#include <type_traits>

struct A { };
struct B : A { };

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

輸出
is_convertible:
int => float: true
int => const int: true
A => B: false
B => A: true


另見