You cannot declare additional overloads for a member function of a class outside the class definition. The language does not provide syntax for it. Since you can't modify the class definition in the standard library headers, you cannot add additional overloads to them either.
Free functions in the std
namespace may not be overloaded. There is syntax for it (the same way any overloads of free functions work), but doing so causes undefined behavior, because it is explicitly forbidden in the standard, see [namespace.std]/1.
What is usually (exceptions apply) allowed is to define template specializations for entities in the std
namespace, but only if the specialization depends on a user-defined type and not for member functions, member function templates or member class templates. See the rest of the quoted and following paragraph linked above.
A common example where specialization inside the std
namespace is used is std::hash
for use as hash function in unordered associative containers:
struct my_type {
//...
};
namespace std {
template<>
struct hash<my_type> {
auto operator()(my_type const& x) const noexcept {
//...
}
};
}
But even something like that is not allowed if my_type
is replaced by int*
, int[3]
or std::array<int, 3>
or anything similar, because there is no dependence on a user-declared type.
If you want to make a std::array
from a built-in array, you will be able to use std::to_array
in C++20:
set<array<int, 3>> s;
int ar[3] = {1,2,3};
s.insert(std::to_array(ar));
auto it = s.find(std::to_array(ar));
Before C++20, you might have the function available as std::experimental::to_array
in #include<experimental/array>
or you can define it yourself (from cppreference.com; requires #include<type_traits>
, #include<utility>
and #include<cstddef>
):
namespace detail {
template <class T, std::size_t N, std::size_t... I>
constexpr std::array<std::remove_cv_t<T>, N>
to_array_impl(T (&a)[N], std::index_sequence<I...>)
{
return { {a[I]...} };
}
}
template <class T, std::size_t N>
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N])
{
return detail::to_array_impl(a, std::make_index_sequence<N>{});
}