Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: C++: Two functions with same name dilema  (Read 11353 times)

0 Members and 1 Guest are viewing this topic.

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
C++: Two functions with same name dilema
« on: April 09, 2013, 07:24:23 am »
I want to make my own library that has functions. I'm very particular about function names, but heres the thing, some of the function names I want to use are already used in another library/file that is included in the program.

Thing is, I don't want to modify included libraries that are not mine or affect how they work (since I will need to use their version of the function too).

In a nutshell it's like this (example!):

External Library has a round() function (for rounding numbers).
My Library also has a round() function which USES the round() function from the external library (mine uses it, but works slightly differently)!

.. and then, in my main.cpp program, if I call round() I want it to know I'm referring to MY library (not the other 'External' library). And I don't want to have to refer to my functions using long/additional tags like:  myLibrary::round().

If I do use namespace tags etc, anywhere in my code, I'd rather it be for the External library functions. But remember, I don't want to edit their library or cause it to not work anymore (since I'm guessing their library (and maybe several other libraries also makes use of that functions without knowing of any changes/additions in namespaces!). Basically I don't want to screw all the other libraries up which aren't mine.

How can I overcome this??

Thanks in advance,
Tom
« Last Edit: April 09, 2013, 07:40:18 am by tom64 »
Newbie to C++ and SFML

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #1 on: April 09, 2013, 07:30:49 am »
So for example, I want to do something like this:

My Library.h/.cpp:
namespace math{#include <math>}

int round(i){
     return math::round(i)+x;
}
 

main.cpp
using namespace "My Library.h";

int main(){
     // This is calling MY version of round() without needing a namespace
     cout << round(5);
}
 
« Last Edit: April 09, 2013, 07:49:01 am by tom64 »
Newbie to C++ and SFML

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #2 on: April 09, 2013, 08:09:19 am »
What is the name of the function, and in which library is it?

I think you're doing it wrong. You should really use a namespace for your own functions, especially if you have very generic names like "round".
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #3 on: April 09, 2013, 08:11:22 am »
Quote
If I do use namespace tags etc, anywhere in my code, I'd rather it be for the External library functions.
Why? Namespaces are there to be used. And if you can't change the other library (you really can't), then do it right in your library at least.

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #4 on: April 09, 2013, 08:19:28 am »
Quote
What is the name of the function, and in which library is it?
I'm just asking from a general point of view.

Quote
You should really use a namespace for your own functions
Quote
Why? Namespaces are there to be used. And if you can't change the other library (you really can't), then do it right in your library at least.

Because in my main program I won't be using the external functions like 'round' I will be using my own 'round' function. It would be too tedious and messy to have to use myNamespace::round() for every single function from my library (since I plan on making lots of functions and using them often).

EDIT: I think it's kinda silly how C++ works. The more libraries you load the greater the chance of a conflict. They should have designed it so that you, the programmer of your own app/lib get to choose whatever names you want, and any external libs that are not yours should require a namespace if there's a conflict.

I come from other programming languages where I've learned not only the names of functions, but how they work. I want to replicate the writing style of the old language I'm used to in c++.
« Last Edit: April 09, 2013, 08:26:29 am by tom64 »
Newbie to C++ and SFML

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: C++: Two functions with same name dilema
« Reply #5 on: April 09, 2013, 08:24:31 am »
I always use namespaces (std::, boost::, ...) and don't find it tedious. It's a nice way to make functions name unique and easily recognizable, and to make the code much clearer to read.

Use a short namespace, like xyz::, you don't have to use a long myNamespace::.

Don't start doing bad design just because you're lazy, this is a very bad idea ;)
Laurent Gomila - SFML developer

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #6 on: April 09, 2013, 08:26:07 am »
Surely if I can do that I should be technically possible to do it they other way around though?

What if someone wrote a ton a code which worked fine, but then when added a library all of a sudden it conflicted with everything? Surely they wouldn't start re-writing/editing all of their ton of code.. But rather, just simply force a namespace on the newly added library (for certain conflicting functions) when used in their main .cpp?
« Last Edit: April 09, 2013, 08:30:38 am by tom64 »
Newbie to C++ and SFML

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #7 on: April 09, 2013, 08:34:25 am »
Then that person didn't pay attention at using proper namespaces.

Also, for round, are you including <cmath> instead of <math.h>? <cmath> adds the stuff to the std:: namespace.

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #8 on: April 09, 2013, 08:40:16 am »
So you would prefer to type:

xaxis = hsuxz::round(i)+hsuxz::otherFunc();

than

xaxis = round(i)+otherFunc();

??

Quote
Also, for round, are you including <cmath> instead of <math.h>? <cmath> adds the stuff to the std:: namespace.
No, it's just an example. I just want to know how to deal with this problem when I face it, because I know I will be writing lots of function names and I have OCD when it comes to naming things... I don't like the fact that other peoples libraries are telling me how (not) to name my functions...
« Last Edit: April 09, 2013, 08:43:01 am by tom64 »
Newbie to C++ and SFML

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #9 on: April 09, 2013, 08:42:16 am »
Quote
So you would prefer to type:
Yes.

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #10 on: April 09, 2013, 08:44:08 am »
Quote
So you would prefer to type:
Yes.

So you would prefer:
xaxis = TankHeroMemberLibraryIsAwsome::round()..  ;D
Newbie to C++ and SFML

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #11 on: April 09, 2013, 08:47:08 am »
Quote
TankHeroMemberLibraryIsAwsome
No, TankHeroMemberLibraryIsAwesome. Or just tank:: or thl:: or hero::. ;)

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: C++: Two functions with same name dilema
« Reply #12 on: April 09, 2013, 08:50:36 am »
I think it's kinda silly how C++ works. The more libraries you load the greater the chance of a conflict.
This is a false assumption. If library developers all follow the unwritten rule of providing their interfaces within their own unique namespace this would not even be an issue. You are blaming the language for poor design decisions or bad practices on the side of the library developer. That is not fair.

They should have designed it so that you, the programmer of your own app/lib get to choose whatever names you want, and any external libs that are not yours should require a namespace if there's a conflict.
If all libraries provided their interfaces in an enclosing namespace, you can name your functions whatever you want as long as you don't declare them within the namespace of one of the libraries you use, but who does that anyway. That way you wouldn't even have to use the scope resolution operator to access your functions because if you chose to, they would be declared in the global namespace.

I come from other programming languages where I've learned not only the names of functions, but how they work. I want to replicate the writing style of the old language I'm used to in c++.
This is a mistake many people who migrate to C++ make. Many still treat C++ as a "glorified C" or a "Java with more performance" but C++ is more than that. If you really understand the concepts that make it unique and different from the other languages you will realize that there is no point trying to emulate another language in C++ because you aren't really making use of its language features and thus not programming in C++ that way.

Then that person didn't pay attention at using proper namespaces.
Or the library was written for C and nobody cared to do the maintenance work....
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

tom64

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: C++: Two functions with same name dilema
« Reply #13 on: April 09, 2013, 08:56:04 am »
Ok.. so how do I attach namespaces to my own functions if it has an identical name?

my lib file
namespace myLib{
        // My declarations/definitions here
}

main.cpp

using namespace myLib

int main(){
       func()
}

??

I can't get it to work..

Why do I have to use myLib:: if I have 'using namespace' added? It would solve most/all of my problems if I did namespace my functions but could just 'using' it...
« Last Edit: April 09, 2013, 08:57:50 am by tom64 »
Newbie to C++ and SFML

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11028
    • View Profile
    • development blog
    • Email
Re: C++: Two functions with same name dilema
« Reply #14 on: April 09, 2013, 09:03:16 am »
using namespace myLib
Don't use it! :D

namespace exp
{
    int round(float number)
    {
        return static_cast<int>(number); // Bad "rounding" function
    }
}

#include <cmath>
#include <iostream>

int main()
{
    std::cout << exp::round(4.78f) << std::round(4.56) << "OMG" << std::endl;
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything