STL源码解析--初读02

1.空间配置器

std:alloc

allocate

默认的第一级配置器和第二级配置器

2. place new 

#include <new>

template<class T1, class T2>

inline void construct(T1* p, const T2 &value)

{

   new(p) T2(value);

}//在p代表的已经分配的内存出,构造一个新的实例T2(value),所指向的对象为value

3.new-handler

typedef void(*new_handler)()

new_handler set_new_handler (new_handler new_p) throw();
 // new_handler example
#include <iostream> #include <cstdlib> #include <new> using namespace std;void no_memory () {
  cout << "Failed to allocate memory!\n";
  exit (1);
}int main () {
  set_new_handler(no_memory);
  cout << "Attempting to allocate 1 GiB...";
  char* p = new char [1024*1024*1024];
  cout << "Ok\n";
  delete[] p;
  return 0;

}

//new_handler 参数为一个异常处理函数,当new运算符申请分配内存出错时调用这个函数。

4.template<>

template<class T>

template<int,inst>

template<unsigned int N>

template< template<typename T1> class Alooc>

up vote 28 down vote

Yes, it is a non-type parameter. You can have several kinds of template parameters

  • Type Parameters.
    • Types
    • Templates (only classes, no functions)
  • Non-type Parameters
    • Pointers
    • References
    • Integral constant expressions

What you have there is of the last kind. It's a compile time constant (so-called constant expression) and is of type integer or enumeration. After looking it up in the standard, i had to move class templates up into the types section - even though templates are not types. But they are called type-parameters for the purpose of describing those kinds nonetheless. You can have pointers (and also member pointers) and references to objects/functions that have external linkage (those that can be linked to from other object files and whose address is unique in the entire program). Examples:

Template type parameter:

template<typename T> struct Container {     T t; }; // pass type "long" as argument. Container<long> test; 

Template integer parameter:

template<unsigned int S> struct Vector {     unsigned char bytes[S]; }; // pass 3 as argument. Vector<3> test; 

Template pointer parameter (passing a pointer to a function)

template<void (*F)()> struct FunctionWrapper {     static void call_it() { F(); } }; // pass address of function do_it as argument. void do_it() { } FunctionWrapper<&do_it> test; 

Template reference parameter (passing an integer)

template<int &A> struct SillyExample {     static void do_it() { A = 10; } }; // pass flag as argument int flag; SillyExample<flag> test; 

Template template parameter.

template<template<typename T> class AllocatePolicy> struct Pool {     void allocate(size_t n) {         int *p = AllocatePolicy<int>::allocate(n);     } }; // pass the template "allocator" as argument.  template<typename T> struct allocator { static T * allocate(size_t n) { return 0; } }; Pool<allocator> test; 

A template without any parameters is not possible. But a template without any explicit argument is possible - it has default arguments:

template<unsigned int SIZE = 3> struct Vector {     unsigned char buffer[SIZE]; }; Vector<> test; 

Syntactically, template<> is reserved to mark an explicit template specialization, instead of a template without parameters:

template<> struct Vector<3> {     // alternative definition for SIZE == 3 };

你可能感兴趣的:(STL源码解析--初读02)