0

Consider this situation: We write code that works well until we include a header file that happens to have an function overloading one of our original functions. In this case, there maybe some places where the calling of function goes to the overloaded version for better fitting of parameters, which is also logically unexpected. What's worse, compiler doesn't complain.

Here is a demo to clarify my question:

/*
For simlicity, I write the codes in different files in this way;
Whether include header.h behaves differently
*/

// main.cpp   
#include "header.h" // If included, sb will be overloaded
#include<iostream>
void sb(int){std::cout << "int" << endl;}
int main(){
    sb(3.5);
    return 0;
}


// header.h
void sb(double);


// hehe.cpp
void sb(double){cout << "double" << endl;}

I have no idea whether this problem happens frequently or not, and wonder if there is any way to solve or prevent it.

Eagle
  • 93
  • 5
  • 6
    Have you heard of namespaces? – StoryTeller - Unslander Monica Jul 27 '17 at 07:55
  • Use proper programming practices (namespaces, classes, etc.) - putting all functions into the same namespace is a recipe for disaster – UnholySheep Jul 27 '17 at 07:55
  • Why not putting these functions into different namespaces? – Edgar Rokjān Jul 27 '17 at 07:55
  • @Story Teller So do you mean we should use namespace anywhere to "protect" our method from being accidently overloaded? If not, the problem may occur without being noticed? – Eagle Jul 27 '17 at 08:19
  • @UnholySheep I'm still learning C++ and thanks for your advise. So do you mean we should always organize our functions into one namespace or another to prevent this problem in some degree? – Eagle Jul 27 '17 at 08:22
  • 1
    Not just to protect yourselves; to protect anyone who uses your API. Picking a really *cool* name for you functions and classes in the global namespace is a recipe for trouble. You're not the only one who wants to use the best identifiers. – StoryTeller - Unslander Monica Jul 27 '17 at 08:23
  • @StoryTeller Get it. In fact this question comes from whether using "using namespace std;" or not. It seems that this case is similar to name clashing... BTW, do you mean "a recipe of trouble"? Thanks for your help. – Eagle Jul 27 '17 at 08:32
  • 1
    @Eagle - I'm not a native English speaker, so I very well could be :) If you encountered this problem on account of `using namespace std;` then [you really ought to read this](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – StoryTeller - Unslander Monica Jul 27 '17 at 08:34

1 Answers1

0

The simple method is to never let the overloads be a better match. The less simple (but more scalable) method is to apply some organisation to your program, either classes, namespaces or just better function names.

Simple example:

void sb(double);
void sb(int){std::cout << "int" << endl;}
int main(){
    sb(static_cast<int>(3.5));
    return 0;
}

Less simple (but more scalable)

namespace some_namespace { // could be a class
    void sb(double);
}
namespace other_namespace { // also could be a class
    void sb(int){std::cout << "int" << endl;}
}
int main(){
    other_namespace::sb(3.5);
    return 0;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75