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

Thread: basic_string allocator

  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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: basic_string allocator

    First, Visual C++ 6.0 is not an ANSI compliant C++ compiler -- many things that compile there will not compile using an ANSI compliant compiler.

    An ANSI compliant C++ compiler requires that the basic_string allocator have a rebind() function defined.
    Can someone point me to an example of a custom std::basic_string allocator that compiles under VS 2005 or VS 2008?
    MSDN?

    http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 4th, 2009 at 10:36 PM.

  3. #3
    Join Date
    Nov 2001
    Posts
    251

    Re: basic_string allocator

    Thanks, I just had to add one line:

    Code:
     template <typename U> struct rebind { typedef MyAllocator<U> other; };
    and it compiled! Awesome.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center