diff --git a/util/container.hpp b/util/container.hpp index a35d56a6d..41c8821f8 100644 --- a/util/container.hpp +++ b/util/container.hpp @@ -1,6 +1,6 @@ /* -Copyright (c) 2013, Project OSRM, Dennis Luxen, others +Copyright (c) 2015, Project OSRM, Dennis Luxen, others All rights reserved. Redistribution and use in source and binary forms, with or without modification, @@ -25,8 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef CONTAINER_HPP_ -#define CONTAINER_HPP_ +#ifndef CONTAINER_HPP +#define CONTAINER_HPP #include #include @@ -34,12 +34,27 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace osrm { -template void sort_unique_resize(std::vector &vector) +namespace detail { - std::sort(vector.begin(), vector.end()); +// Culled by SFINAE if reserve does not exist or is not accessible +template constexpr auto has_resize_method(T &t) -> decltype(t.resize(0), bool()) +{ + return true; +} + +// Used as fallback when SFINAE culls the template method +constexpr bool has_resize_method(...) { return false; } +} + +template void sort_unique_resize(Container &vector) +{ + std::sort(std::begin(vector), std::end(vector)); const auto number_of_unique_elements = - std::unique(vector.begin(), vector.end()) - vector.begin(); - vector.resize(number_of_unique_elements); + std::unique(std::begin(vector), std::end(vector)) - std::begin(vector); + if (detail::has_resize_method(vector)) + { + vector.resize(number_of_unique_elements); + } } // template inline void sort_unique_resize_shrink_vector(std::vector &vector) @@ -82,4 +97,4 @@ Function for_each_pair(ContainerT &container, Function function) return for_each_pair(std::begin(container), std::end(container), function); } } -#endif /* CONTAINER_HPP_ */ +#endif /* CONTAINER_HPP */