<type_traits>

類模板
<type_traits>

std::add_lvalue_reference

template <class T> struct add_lvalue_reference;
新增左值引用
獲取指向T.

轉換後的類型別名為成員型別的左值引用型別如下

  • 如果已知Tis an object or function type, this isT&.
  • 如果已知Tis an rvalue reference type, this is the lvalue reference that refers to the same type (e.g., forint&&this isint&).
  • Otherwise (i.e.,T的 C++ 等效檔案是voidor already an lvalue reference), it isT不變。

請注意,此類僅使用另一種型別作為模型來獲取型別,但它不會在這些型別之間轉換值或物件。

模板引數

T
一個型別。

成員型別

成員型別定義
型別如果已知Tis an object or function typeT&
如果已知Tis the rvalue reference typeU&&: U&
否則T

示例

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

int main() {
  typedef std::add_lvalue_reference<int>::type A;    // int&
  typedef std::add_lvalue_reference<int&>::type B;   // int&
  typedef std::add_lvalue_reference<int&&>::type C;  // int&
  typedef std::add_lvalue_reference<int*>::type D;   // int*&

  std::cout << std::boolalpha;
  std::cout << "typedefs of int&:" << std::endl;
  std::cout << "A: " << std::is_same<int&,A>::value << std::endl;
  std::cout << "B: " << std::is_same<int&,B>::value << std::endl;
  std::cout << "C: " << std::is_same<int&,C>::value << std::endl;
  std::cout << "D: " << std::is_same<int&,D>::value << std::endl;

  return 0;
}

輸出
typedefs of int&:
A: true
B: true
C: true
D: false


另見