Why was function overloading added to C++?

前端 未结 6 1885
囚心锁ツ
囚心锁ツ 2021-02-03 21:03

I have a C background. I was just wondering why was function overloading added to C++? C doesn\'t have function overloading but C++ does, what was the need for it?

What

相关标签:
6条回答
  • 2021-02-03 21:24

    One good reason, in addition to what DeadMG said, is that if you're writing a template function which e.g. calls sqrt, then you need a generic way of calling sqrt -- it would be very difficult if you had to try and somehow vary the name to sqrtf, sqrtd, etc., depending on the type of the template parameter. Overloading solves this problem, because then you just write sqrt and let the compiler figure out which overload it should be using:

    template <typename T>
    T sqrt_plus_one(T t) // contrived example
    {
      return sqrt(t) + 1;
    }
    
    0 讨论(0)
  • 2021-02-03 21:27

    Using polymorphism , we can design a family of functions with same function name but with different argument list . The function would perform different operations depending on the arguments list in the function call.

    0 讨论(0)
  • 2021-02-03 21:28

    Would you prefer "selecting" one among abs/labs/llabs/fabs/fabsf/fabsl Or just abs()?

    Obviously, abs().

    So function overloading is a relief for programmers, most of the time, beside other advantages.

    0 讨论(0)
  • 2021-02-03 21:28

    Try to come up with a comfortable way to construct objects if it weren't for function overloading.

    std::string foo = "bar";
    std::vector< std::string > myStringVector;
    myStringVector.push_back( std::string() );
    myStringVector.push_back( std::string( "hello" ) );
    myStringVector.push_back( std::string( foo ) );
    

    A nonsense example, of course, but it illustrates the point.

    Another point would be template programming. You could not come up with generic templates if you had to have a different function name for each parameter type.

    0 讨论(0)
  • 2021-02-03 21:39

    It increases maintainability. If you have a type T and you call a function with it, then you need to change T, if the function has been overloaded for the new T then you can recompile instantly. In C you would have to go back and dig through all the call sites and change the function called. Take sqrt(). If you want to sqrt() a float, then you have to change to sqrtf().

    Not just that, but the volume and complexity of C++'s type system is far more than in C, and having to have separate function names for every possible overload would quickly exhaust the reasonable pool of names for functions that serve the same purpose but take different arguments, because now there's a lot more arguments to take.

    For example, compare the C and C++ string libraries. The C string library offers one method to append to a string - strcat(). C++'s std::string::append has eight overloads. What do you want to call them? append_a, append_b, etc? That's ridiculous- they all serve the same function, just in different ways.

    Edit: It is actually worth mentioning that append is a really bad example, many of the C++ string overloads are very redundant. However, this is more general case than that and not all of those overloads redundant.

    0 讨论(0)
  • 2021-02-03 21:51

    You could get the answer straight from the horse's mouth: The Design and Evolution of C++ by Bjarne Stroustrup devotes an entire chapter to overloading, its history, evolution, design tradeoffs and decisions.

    I won't recount the story here, but will mention a couple of interesting historical facts:

    • operator and function overloading are closely related;
    • in early C++ there used to be a special keyword (overload) that had to be used to declare an identifier as overloaded;
    • function overloading requires type-safe linking (i.e. name mangling); when first implemented, it helped discover a surprising number of link-time errors in existing C and C++ code (to quote Stroustrup, it was like "running lint on a C program for the first time -- somewhat embarrassing".)
    0 讨论(0)
提交回复
热议问题