<type_traits>

類模板
<type_traits>

std::is_literal_type

template <class T> struct is_literal_type;
字面量型別

標識 T 是否為字面量型別的特性類。

一個 字面量型別 是一個可以作為constexpr的型別。這對於 標量 型別、引用、某些類以及任何此類型別的陣列都是成立的。

字面量型別的類是指(使用, struct聯合體定義)
  • 具有平凡解構函式,
  • 所有建構函式呼叫和任何具有花括號或等號初始化的非靜態資料成員都是常量表達式,
  • 是*聚合型別*,或至少有一個constexpr建構函式或建構函式模板,但不是複製或移動建構函式,並且
  • 所有非靜態資料成員和基類都是*字面量型別*

is_literal_type繼承自 integral_constant,其值為 true_typefalse_type,具體取決於T是否為平凡型別。

模板引數

T
一個完整型別,或者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
// is_literal_type example
#include <iostream>
#include <type_traits>

struct A { };
struct B { ~B(){} };

int main() {
  std::cout << std::boolalpha;
  std::cout << "is_literal_type:" << std::endl;
  std::cout << "int: " << std::is_literal_type<int>::value << std::endl;
  std::cout << "int&: " << std::is_literal_type<int&>::value << std::endl;
  std::cout << "int*: " << std::is_literal_type<int*>::value << std::endl;
  std::cout << "A: " << std::is_literal_type<A>::value << std::endl;
  std::cout << "B: " << std::is_literal_type<B>::value << std::endl;
  return 0;
}

輸出
is_literal_type:
int: true
int&: true
int*: true
A: true
B: false


另見