'C++03 equivalent for auto in the context of obtaining an allocator

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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jerry Coffin