Home > front end >  C 03 equivalent for auto in the context of obtaining an allocator
C 03 equivalent for auto in the context of obtaining an allocator

Time:03-05

auto source_allocator = source.get_allocator();

Is there a way of replacing auto in the above with something C 03/98-friendly, in the event where the type of allocator is not known in advance?

CodePudding user response:

Based on what you posted in the comments, it sounds like you're trying to do this inside a template.

Assuming you are expecting your templated type to be a std::container, or something compatible, you can do this: typename T::allocator_type in place of auto.

https://godbolt.org/z/Pda77vjox

CodePudding user response:

At least for standard containers, the container type is required to define container_type, so you'd normally end up with something on this general order:

template <class Container>
void foo(Container const &source) { 
    typename Container::allocator_type source_allocator = source.get_allocator();
    // ...
}

This is exactly the sort of situation for which they required standard containers to define those names. Of course, it also works fine with other containers that do the same.

  • Related