custom Allocator compilation difficulties

I am trying to create a custom allocator, but there are compilation problems. By changing the value of the define from

#define _GLIBCXX_FULLY_DYNAMIC_STRING 0

to

#define _GLIBCXX_FULLY_DYNAMIC_STRING 1

I managed to switch from being unable to compile to being able to compile, but should it be like that? Shouldn't it be a little bit simpler ?

Does any body has experienc on that and already knows how these compilation problems may be solved.

The code the minimal required:

#include <bits/c++config.h>
#define _GLIBCXX_FULLY_DYNAMIC_STRING 0

#include <stdint.h>
#include <stddef.h>

#include <memory>
#include <string>
#include <limits>


typedef int32_t Token;
typedef unsigned char byte;

using namespace std;
template <typename T>
struct Allocator {
 public:
    static const size_t heapSize=0x1000;
    static size_t freePos;
    static Token freeT;
    static byte m[heapSize];

// http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement

    typedef T value_type;

    typedef value_type* pointer;typedef const value_type* const_pointer;

    typedef value_type& reference;typedef const value_type& const_reference;

    typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;

  template<typename U> struct rebind {typedef Allocator<U> other;};

    inline explicit Allocator() {freeT=0;freePos=0;}
  inline ~Allocator() {}
  inline Allocator(Allocator const&) {} // with explicit it doesn't compile

    //template<typename U>
  //inline explicit Allocator(Allocator<U> const&) {}

  inline pointer address(reference r) {return &r;}
  inline const_pointer address(const_reference r) {return &r;}

  static inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0){ 
        return reinterpret_cast<pointer>(::operator new(cnt * sizeof (T))); 
/*  pointer allocate(size_type n, const_pointer hint = 0 ){
        size_t t=freePos;freePos+=sizeof(T)*n;return t;
    }
*/
  }

    static inline void deallocate(pointer p, size_type){
        ::operator delete(p);
/*  pointer deallocate(pointer p,size_type n){
        size_t sz=sizeof(T)*n;
        *(size_t*)(m+p)=sz;
    }
*/
    }

  inline size_type max_size() const{ 
        return std::numeric_limits<size_type>::max() / sizeof(T);
    }
  inline void construct(pointer p, const T& t) { new(p) T(t); }

/*
  void              construct(pointer p, const T& val) 
                    { new ((T*) p) T(val); }
*/
  inline void destroy(pointer p) { p->~T(); }
//   void              destroy(pointer p) { ((T*)m[p])->~T(); }

  inline bool operator==(Allocator const&) {return true;}
  inline bool operator!=(Allocator const& a) {return false;}
};

#endif


using namespace std;

typedef     std::basic_string< char,std::char_traits<char>,Allocator<char> > String;

int main(){
String s("Nice-the-data-goes-in-my-memory");
return 0;
}

Answers


It works fine if you make the comparison operators free functions. In general, relational operators should be free functions so they can convert implicitly on both sides:

template <typename T>
bool operator==(Allocator<T> const &, Allocator<T> const &) { return true; }

template <typename T>
bool operator!=(Allocator<T> const &, Allocator<T> const &) { return false; }

Need Your Help

How can i make a New intent for a button to open a new Class?

android eclipse android-intent

Here is My Code,I need a to put something inside{} to link the button to new class or activity like second page.java :

Prevent window from being shown by another application

winapi

I am using the ShowWindow function in Windows to show and hide specific windows which are not created by my own application. The use of this function is simple, as you basically only have to pass the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.