首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

《stl源码辨析》空间配置器代码编译不过

2012-12-29 
《stl源码剖析》空间配置器代码编译不过《stl源码剖析》 p46#include stdafx.h#include new#include cstd

《stl源码剖析》空间配置器代码编译不过
《stl源码剖析》 p46


#include "stdafx.h"
#include <new>
#include <cstddef>
#include <cstdlib>
#include <climits>
#include <vector>
#include <iostream>
using namespace std;
namespace JJ
{
 template<class T>
 inline T* _allocate(ptrdiff_t size, T*)
 {
  T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
  if (tmp == 0)
  {
   std::cout<<"out of memorry"<<std::endl;
   exit(1);
  }
  return tmp;
 }

 template<class T>
 inline void _deallocate(T* buffer)
 {
  ::operator delete(buffer);
 }

 template<class T1, class T2>
 inline void _construct(T1* p, const T2& value)
 {
  new(p) T1(value);
 }

 template<class T>
 inline void _destroy(T* ptr)
 {
  ptr->~_T();
 }

 template <class T>
 class allocator
 {
 public:
  typedef T value_type;
  typedef T* pointer;
  typedef const T* const_pointer;
  typedef T& reference;
  typedef const T& const_reference;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;

  template <class U>
  struct rebind
  {
   typedef  allocator<U> other;
  };

  pointer allocate(size_type n, const void* hint = 0)
  {
   return _allocate((difference_type)n, (pointer)0);
  }

  void deallocate(pointer p, size_type n)
  {
   _deallocate(p);
  }

  void construct(pointer p, const T& value)
  {
   _construct(p, value);
  }

  void destroy(pointer p)
  {
   _destroy(p);
  }

  pointer address(reference x)
  {
   return (pointer)&x;
  }

  const_pointer const_address(const_reference x)
  {
   return (const_pointer)&x;
  }

  size_type max_size() const
  {
   return size_type(UINT_MAX / sizeof(T));
  }
 };
}


void main()
{
 int ia[5] = {0, 1, 2, 3, 4};
 unsigned int i;

 std::vector<int, JJ::allocator<int> > iv(ia, ia + 5);
 for (i = 0; i < iv.size(); i++)
 {
  std::cout<<iv[i]<<" ";
  std::cout<<std::endl;
 }
}

错误信息为
错误error C2440: 'initializing' : cannot convert from 'JJ::allocator<T>' to 'JJ::allocator<T>'c:\program files\microsoft visual studio 10.0\vc\include\vector454



[解决办法]
Stl源码剖析上的代码需要在SGI上编译,话说SGI是什么。。。
你看一下vector的源码,应该是这个配置器的函数名字无法匹配

热点排行