類模板
<memory>

std::uses_allocator

template <class T, class Alloc> struct uses_allocator;
Uses allocator

Trait class that identifies whether T accepts an allocator convertible from Alloc.

它繼承自 integral_constant,並根據Taccepts such allocator.

The unspecialized definition for this template inherits from true_type ifT::allocator_typeexists andis_convertible<Alloc,T::allocator_type>::valueis notfalse.

Classes that do not fill this requirement but still use an allocator shall specialize this template to derive from true_type if either
  • the last argument of a constructor has type Alloc.
  • or, the first argument of a constructor has type allocator_arg_t and the second argument has type Alloc.

模板引數

T
一個型別。
Alloc
An allocator type.

成員型別

Inherited from integral_constant
成員型別定義
value_typebool
型別true_type 或 false_type

成員常量

Inherited from integral_constant
成員常量定義
trueorfalse

成員函式

Inherited from integral_constant

示例

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

int main() {
  typedef std::vector<int> Container;
  typedef std::allocator<int> Allocator;

  if (std::uses_allocator<Container,Allocator>::value) {
    Allocator alloc;
    Container foo (5,10,alloc);
    for (auto x:foo) std::cout << x << ' ';
  }
  std::cout << '\n';
  return 0;
}

輸出
10 10 10 10 10 


另見