CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Nov 2001
    Posts
    251

    basic_string allocator

    Can someone point me to an example of a custom std::basic_string allocator that compiles under VS 2005 or VS 2008?

    I can't get this code to compile. All errors refer to a file called xstring. The following code might have worked
    under MSVC++ 6.0, but apparently something has changed in basic_string since then.

    Code:
    #include <string>
    
    template<class T>
    struct MyAllocator {
    
     typedef T* pointer; 
     typedef const T* const_pointer; 
     typedef T& reference; 
     typedef const T& const_reference; 
     typedef T value_type; 
     typedef size_t size_type; 
     typedef ptrdiff_t difference_type;
    
     pointer address(reference x) const { return (&x); } 
     const_pointer address(const_reference x) const { return (&x); } 
    
     pointer allocate(size_type n, const_pointer = 0) {
        return (pointer)::operator new(n * sizeof(T));
     }
      void deallocate(pointer p, size_type n) {
        ::operator delete(p);
     }
    };
    
    
    typedef std::basic_string<char, std::char_traits<char>, MyAllocator<char> > MyString;
    
    
    int
    main(int argc, char *argv[], char *env[]) {
    
     MyString str;
    
     return 0;
    }

    Code:
    C:\Program Files\Microsoft Visual Studio 8\VC\include\xstring(467) : error C2903: 'rebind' : symbol is neither a class template nor a function template
            C:\Program Files\Microsoft Visual Studio 8\VC\include\xstring(496) : see reference to class template instantiation 'std::_String_val<_Ty,_Alloc>' being compiled
            with
            [
                _Ty=char,
                _Alloc=MyAllocator<char>
            ]
            ..\main.cpp(37) : see reference to class template instantiation 'std::basic_string<_Elem,_Traits,_Ax>' being compiled
            with
            [
                _Elem=char,
                _Traits=std::char_traits<char>,
                _Ax=MyAllocator<char>
            ]
    C:\Program Files\Microsoft Visual Studio 8\VC\include\xstring(467) : error C2039: 'rebind' : is not a member of 'MyAllocator<T>'
            with
            [
                T=char
            ]
     etc...
    Last edited by Syslock; October 5th, 2009 at 11:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured