STL源碼解析--初讀02

1.空間配置器ios

std:allocexpress

allocateapp

默認的第一級配置器和第二級配置器less

2. place new 函數

#include <new>post

template<class T1, class T2>spa

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

{orm

   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

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 integer parameter:

 
      

Template pointer parameter (passing a pointer to a function)

 
      

Template reference parameter (passing an integer)

 
      

Template template parameter.

 
      

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

 
      

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

 
     
#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>template<typename T> struct Container {     T t; }; // pass type "long" as argument. Container<long> test; template<unsigned int S> struct Vector {     unsigned char bytes[S]; }; // pass 3 as argument. Vector<3> test; 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<int &A> struct SillyExample {     static void do_it() { A = 10; } }; // pass flag as argument int flag; SillyExample<flag> test; 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; template<unsigned int SIZE = 3> struct Vector {     unsigned char buffer[SIZE]; }; Vector<> test; template<>template<> struct Vector<3> {     // alternative definition for SIZE == 3 };
相關文章
相關標籤/搜索