Overloading STL function

c++ overloaded functions
function overloading
overloading in c++
c++ program to add two vectors using operator overloading
function overloading in c++ using class
overloading the function operator mcq
function call operator overloading in c++
what do you mean by overloading of a function

I have a code like this:

   set<array<int, 3>> s;
   int ar[3] = {1,2,3};
   auto it = s.find(ar);

And the message of IDE that no instance of overloaded function insert/find. If I use std::array it is ok, ::insert and ::find work in the same way. But what if i wanna use these C arrays T[N] or find function with std::array in set should return iterator if two of three elements equal. Or set::insert will not insert a new array if it is a permutation of exist, I mean s.insert({1,2,3}) - already added, s.insert({3,2,1}) - return iterator on {1,2,3}.

The question is how to overload STD functions? Not in this code especially but in general? Any link or real code example welcome. May be its a really simple question for experienced programmers) but there are a lot of examples of operator overloading, but no for STL. Thanks.

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 {
    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};


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>{});

How to Use Overloaded Functions With The STL, But there is one case where we can't apply STL algorithms right out of the box: when the function passed has overloads. Here is an example to� Overloading stream insertion (<>) operators in C++. In C++, stream insertion operator “<<” is used for output and extraction operator “>>” is used for input. We must know following things before we start overloading these operators. 2) These operators must be overloaded as a global function.

About the other question - about permutations, i.e., that you want your set to find iterator to {1,2,3} when you look for a permutation of it.

Technically, you can do 2 things:

(1) make a specialized comparison operator for which permutations are equivalent. Or make a class that wraps std::array and implement its comparison operators this way.

(2) (preferred) simply sort your array before adding it to set and/or searching for it.

If you need its elements in original order, then I recomend to make utility class that contains both the original array and sorted version that it uses for comparisons.

If you also want to be able to add option for having multiple original while returning the exact one when it exists... then its more complicated. It can be done with some composite functions.

Operator overloading in C++ to print contents of vector, map, pair , by overloading "<<" operator. #include <iostream>. #include <vector>. using namespace std;. // C++ template to print vector container elements. Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++. Following is a simple C++ example to demonstrate function overloading. filter_none. edit.

Just like @walnut said, you cannot declare additional overloads for a member function of a class outside the class definition. Other than encapsulate your own int<3> class, there is another work around - operator << override.

set<array<int, 3>>& operator << (set<array<int, 3>>& s, int ar[3])
    array<int,3> ar2 { ar[0], ar[1], ar[2] };

    return s;

It looks weird, but easy to use.

Now you can do this

set<array<int, 3>> s;
int ar[3] = {1,2,3};

s << ar;

However, if there is a bunch of functions you want to use in set against int[3] against array, It is better to define your own class / struct.

template <class T, int arsize>
struct MyFixedArray
    T ar[arsize];
    MyFixedArray(T _ar[arsize]) {
        for (int i = 0; i < arsize; ++i)
            ar[i] = _ar[i];
    operator array<T, arsize>() const {
        array<T, arsize> _ar;
        for (int i = 0; i < arsize; ++i)
            _ar[i] = ar[i];

        return _ar;

and now you can use it freely everwhere

MyFixedArray<int, 3> myar(ar);

auto it = s.find(myar);

Function Call Operator () Overloading in C++, Following example explains how a function call operator () can be overloaded. Live Demo. #include <iostream> using namespace std� Functions. The STL includes classes that overload the function call operator. Instances of such classes are called function objects or functors. Functors allow the working of the associated function to be customized with the help of parameters to be passed. Functors; Iterators . As the name suggests, iterators are used for working upon a sequence of values.

I don't think that C++ gives you the right to do so. For more information, you may get this answer useful in your case. I hope that helps your problem.

Overload resolution - cppreference.com, For other contexts where overloaded function names can appear, see to generate all comparisons between std::string and const char*, both� C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).

Operator Overloading, C++ FAQ, What are some examples of operator overloading? � Δ � myString + yourString might concatenate two std::string objects � myDate++ might increment a Date object� Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is different from the

Overloads and templates - C++ Tutorials, overloading functions #include <iostream> using namespace std; int operate ( int a, int b) Note that a function cannot be overloaded only by its return type. 1) For operator overloading to work, at least one of the operands must be a user defined class object. 2) Assignment Operator: Compiler automatically creates a default assignment operator with every class. The default 3) Conversion Operator: We can also write conversion operators that can be

C++ Tutorial: Operator Overloading I - 2020, C++ Tutorial: Operator Overloading, functions, default arguments, function # include <iostream> int main() { using namespace std; MyComplex c1(5,10);� Function overloading is usually associated with statically-typed programming languages that enforce type checking in function calls. An overloaded function is really just a set of different functions that happen to have the same name. The determination of which function to use for a particular call is resolved at compile time.

  • Why not declare an array<int,3> ar{ {1,2,3} } in your function instead of an int ar[3]?
  • Yeap, it works that way. But i want to understand some things about overloading STL at all.
  • Note that the days of STL are gone. It has now become a part of the standard library.
  • We can use the unordered map/set container with specialized version of hashing en.cppreference.com/w/cpp/utility/hash
  • @Evg -- "should .. be used"? No. "is widely used"? Yes. :-(
  • Thats because i cant upvote answer. Not enough reputation.
  • Thanks! Utility class i think the best decision in this case.
  • I`m trying to compile and it works:) Сould you clarify about the insert operation here? I mean if we using is set<int>, set::insert trying to find existing element, compare by key value, if there is no equal insert new element of set, if equal return iterator. If it set<std::array<int,n>> set::insert overloaded in standard library, and trying to find full equality of each[n] position in array and make the same. But here we pass the value of MyFixedArray class. How this conversion to array<int,n> happen? If will be two varaible like T ar[arsize] and T ar2[arsize]; Which will be used?
  • operator array<T, arsize>() const { … } is the conversion operator override. When you use set<array<int, 3>>::insert, it is looking for array<int, 3> as a parameter, but you passed in MyFixedArray<int, 3>. It has a conversion operator to array<int, 3>, so it will be called to do the conversion, then pass to insert.
  • Thank you. As i understand this : "A program may add a template specialization for any standard library template..." mean that std::set::find - have template version and i can create specialization?, and [std::set::insert] (en.cppreference.com/w/cpp/container/set/insert) - do not have. Of course i can create new container and add in it any features, but without c++ STL perfomance
  • @AlexeyChicherin, you can't. The template version of .find is there to support transparent comparators. See this question.