Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68317
| From | Manfred <noname@add.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: On stateful allocators and GCC |
| Date | 2019-09-11 15:02 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <qlar8k$ni2$1@gioia.aioe.org> (permalink) |
| References | <05d0355f-1bce-49a1-b8c5-decc89cfe8e5@googlegroups.com> <qladau$1d1o$2@adenine.netfront.net> <f9e5e210-13ac-42c2-a99c-d3980a55f6b6@googlegroups.com> |
On 9/11/2019 1:32 PM, Daniel wrote:
> On Wednesday, September 11, 2019 at 5:04:43 AM UTC-4, Juha Nieminen wrote:
>> Daniel wrote:
>>> I have no issues with vs or clang.
>>
>> I tried this with clang, and it gives an error. (If the constructor is
>> commented out, it compiles and works ok.) Am I missing something obvious?
>>
>> //-----------------------------------------------------
>> #include <iostream>
>> #include <string>
>>
>> struct MyAlloc
>> {
>> using value_type = char;
>> MyAlloc(int) {}
>> char* allocate(std::size_t n) { return new char[n]; };
>> char* allocate(std::size_t n, const void*) { return new char[n]; };
>> void deallocate(char* ptr, std::size_t) { delete[] ptr; }
>> bool operator==(const MyAlloc&) const { return true; }
>> bool operator!=(const MyAlloc&) const { return false; }
>> };
>>
>> int main(int argc, char** argv)
>> {
>> std::basic_string<char, std::char_traits<char>, MyAlloc> str("hello");
>> std::cout << str << "\n";
>> }
>> //-----------------------------------------------------
>
> For a stateful allocator, you need to pass an instance of the constructor
> to the object.
You probably meant to pass an instance of the allocator.
In C++ an instance of the constructor is ungood.
But MyAlloc also isn't right, it needs to be a template class,
> and have a constructor that supports rebinding. As a minimal example, try
>
> #include <iostream>
> #include <string>
> #include <memory>
>
> template <class T>
> struct MyAlloc
> {
> using value_type = T;
> using size_type = std::size_t;
> using propagate_on_container_move_assignment = std::true_type;
>
> MyAlloc(int) {}
>
> template< class U >
> MyAlloc(const MyAlloc<U>&) noexcept {}
>
> T* allocate(size_type n)
> {
> return static_cast<T*>(::operator new(n * sizeof(T)));
> }
>
> void deallocate(T* ptr, size_type) noexcept
> {
> ::operator delete(ptr);
> }
>
> bool operator==(const MyAlloc&) const { return true; }
> bool operator!=(const MyAlloc&) const { return false; }
> };
>
> int main()
> {
> std::basic_string<char, std::char_traits<char>, MyAlloc<char>> str("hello", MyAlloc<char>(1));
> std::cout << str << "\n";
> }
>
>
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
On stateful allocators and GCC Daniel <danielaparker@gmail.com> - 2019-09-09 20:54 -0700
Re: On stateful allocators and GCC Melzzzzz <Melzzzzz@zzzzz.com> - 2019-09-10 04:00 +0000
Re: On stateful allocators and GCC James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-10 08:56 -0400
Re: On stateful allocators and GCC Öö Tiib <ootiib@hot.ee> - 2019-09-10 00:25 -0700
Re: On stateful allocators and GCC Öö Tiib <ootiib@hot.ee> - 2019-09-10 01:04 -0700
Re: On stateful allocators and GCC Daniel <danielaparker@gmail.com> - 2019-09-10 04:51 -0700
Re: On stateful allocators and GCC Daniel <danielaparker@gmail.com> - 2019-09-11 04:39 -0700
Re: On stateful allocators and GCC Juha Nieminen <nospam@thanks.invalid> - 2019-09-11 09:04 +0000
Re: On stateful allocators and GCC Daniel <danielaparker@gmail.com> - 2019-09-11 04:32 -0700
Re: On stateful allocators and GCC Manfred <noname@add.invalid> - 2019-09-11 15:02 +0200
Re: On stateful allocators and GCC Daniel <danielaparker@gmail.com> - 2019-09-11 06:33 -0700
csiph-web