SFML community forums

Help => General => Topic started by: tom64 on April 09, 2013, 07:24:23 am

Title: C++: Two functions with same name dilema
Post by: tom64 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
Title: Re: C++: Two functions with same name dilema
Post by: tom64 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);
}
 
Title: Re: C++: Two functions with same name dilema
Post by: Laurent 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".
Title: Re: C++: Two functions with same name dilema
Post by: Tank 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.
Title: Re: C++: Two functions with same name dilema
Post by: tom64 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++.
Title: Re: C++: Two functions with same name dilema
Post by: Laurent 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 ;)
Title: Re: C++: Two functions with same name dilema
Post by: tom64 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?
Title: Re: C++: Two functions with same name dilema
Post by: Tank 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.
Title: Re: C++: Two functions with same name dilema
Post by: tom64 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...
Title: Re: C++: Two functions with same name dilema
Post by: Tank on April 09, 2013, 08:42:16 am
Quote
So you would prefer to type:
Yes.
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 08:44:08 am
Quote
So you would prefer to type:
Yes.

So you would prefer:
xaxis = TankHeroMemberLibraryIsAwsome::round()..  ;D
Title: Re: C++: Two functions with same name dilema
Post by: Tank on April 09, 2013, 08:47:08 am
Quote
TankHeroMemberLibraryIsAwsome
No, TankHeroMemberLibraryIsAwesome. Or just tank:: or thl:: or hero::. ;)
Title: Re: C++: Two functions with same name dilema
Post by: binary1248 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....
Title: Re: C++: Two functions with same name dilema
Post by: tom64 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...
Title: Re: C++: Two functions with same name dilema
Post by: eXpl0it3r 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;
}
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:05:32 am
Why am I getting errors.. Here's the code I'm using. I have two simple test libraries included: lib1.h and lib2.h

main.cpp
#include "lib1.h"
#include "lib2.h"
#include <iostream>

using namespace LIB1;

int main(){
        std::cout << func() << std::endl;
        return 0;
}

lib1.h
#pragma once

namespace LIB1{
        int func(void);
}

lib1.cpp
#include "lib1.h"

namespace LIB1{
        int func(){
                return 1;
        }
}

lib2.h
#pragma once

int func(void);
 

lib2.cpp
#include "lib2.h"

int func(){
        return 0;
}
 
Title: Re: C++: Two functions with same name dilema
Post by: eXpl0it3r on April 09, 2013, 09:08:16 am
using namespace LIB1;
DON'T. USE. IT. :D

Also Lib2 is doing it wrong. If it's one of your library, you should change it and let it use its own namespace. ;)
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:08:49 am
eXpl0it3r, if that code works, then can't I just do 'using namespace exp' which would answer the very question everyone was telling me I couldn't do...???
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:10:54 am
Quote
Also Lib2 is doing it wrong. If it's one of your library, you should change it and let it use its own namespace.

AAHAHAAARRRGGHH IM SO CONFUSE.

But lib2 is an example of someone elses library that has a function. lib1 is an example of my library where I force a namespace... ????????????????????????

The point of this topic was to ask how to make a function that has the same name and use it without namespace::
Title: Re: C++: Two functions with same name dilema
Post by: eXpl0it3r on April 09, 2013, 09:11:19 am
eXpl0it3r, if that code works, then can't I just do 'using namespace exp' which would answer the very question everyone was telling me I couldn't do...???
If both libraries use their own namespace (like exp and std do), then you potentially could do that, BUT you shouldn't be using namespace and the code posted by you, the second library doesn't have its own namespace.
Title: Re: C++: Two functions with same name dilema
Post by: Laurent on April 09, 2013, 09:13:09 am
To be clear: writing "using namespace LIB1" cancels the whole namespace LIB1, and you end up in the exact same situation: all LIB1 functions are again in the global namespace. That doesn't solve any conflict.

You won't solve your problem with such tricks. You have to modify you whole code to use namespaces (for your own functions) properly. There's no other choice.
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:14:25 am
Quote
and the code posted by you, the second library doesn't have its own namespace.

Yes... but isn't that exactly what many libraries do? Like in the <math> library you just simply use the function 'round()' as is. You don't need a namespace::..

That's why I'm using that as an example.

So in other words, I can't make my own library with simple function names, because I will hit someone elses and therefore need to put ugly xxx:: in front of everything..  :'(
Title: Re: C++: Two functions with same name dilema
Post by: eXpl0it3r on April 09, 2013, 09:15:10 am
AAHAHAAARRRGGHH IM SO CONFUSE.
If you decalre those two functions:
namespace exp
{
    void funct() {}
}

void funct {};
Everything works fine, since the one funct is in the exp namespace and the other funct is in the global namespace, BUT if you now go and do:

namespace exp
{
    void funct() {}
}

void funct {};

using namespace exp;
You're essentially pulling the first function from the exp namespace into the global namespace, BUT there exists already a function called funct in the global namespace -> name clashing.
So don't use using namespace and you're fine. ;D
Title: Re: C++: Two functions with same name dilema
Post by: Tank on April 09, 2013, 09:18:13 am
Quote
That's why I'm using that as an example.
But those are C era functions, it's a bad example.
Title: Re: C++: Two functions with same name dilema
Post by: Laurent on April 09, 2013, 09:20:59 am
All C functions are brought into the std:: namespace if you include the C++ version of the header (<cxxx> instead of <xxx.h>). And guess why they did that... to solve your problem ;)
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:21:06 am
I still dont get why it's possible to namespace my functions, but not possible to namespace someone elses functions?

Can't I just wrap the #include "lib2.h" inside a namespace?

That way, my function will be global, and the other (<math>) will be under a namespace?
Title: Re: C++: Two functions with same name dilema
Post by: Laurent on April 09, 2013, 09:22:35 am
The external library was not compiled with the additional namespace that you want to add to its headers. So if you do that, the linker will complain about every function of this library ('unresolved external symbol: xxx::round').

It can work only if everything is inlined (i.e. the full code is in the headers, there's no compiled library).
Title: Re: C++: Two functions with same name dilema
Post by: eXpl0it3r on April 09, 2013, 09:22:59 am
When you're within a namespace, you'll be using the function within this namespace, if you need the global function name, you'll have to use ::funct(). Here's an example with 3 different foo functions.

#include <iostream>

void foo()
{
        std::cout << "Hello" << std::endl;
}

namespace exp
{
        void foo()
        {
                std::cout << "eXpl0it3r says: Hello" << std::endl;
        }

        void bar()
        {
                foo();
        }

        void bar2()
        {
                ::foo();
        }
}

namespace hax
{
        void foo()
        {
                std::cout << "Tank is hax!" << std::endl;
        }

        void bar()
        {
                foo();
                exp::foo();
                ::foo();
        }
}


int main()
{
        foo();
        exp::foo();
        exp::bar();
        exp::bar2();
        hax::foo();
        hax::bar();

        std::cin.get();
}
 

Output:
Hello
eXpl0it3r says: Hello
eXpl0it3r says: Hello
Hello
Tank is hax!
Tank is hax!
eXpl0it3r says: Hello
Hello

If you still don't get it, then I might not understand, what you don't understand... :-\
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:26:52 am
I totally understand!!!

I just don't get why they never added a feature to C++ to say:
"we are going to rename the function round() from <math> to math_round() only in 'this' .cpp script"

What if someone elses library had the same function name and the same namespace name? It just seems like a clumsy design for a language...

PS: Thanks very much eXpl0it3r for that example, that helps alot.

I suppose I'll just have to bite the bullet. Either create unique function names, or add a namespace to them.

Thanks all for your help.  :) :'(
Title: Re: C++: Two functions with same name dilema
Post by: eXpl0it3r on April 09, 2013, 09:30:40 am
I just don't get why they never added a feature to C++ to say:
"we are going to rename the function round() from <math> to math_round() only in 'this' .cpp script"
You're using the wrong header, as stated before.
It should be: #include <cmath>, which puts everything into the std namespace. ;)

PS: Thanks very much eXpl0it3r for that example, that helps alot.
You're welcome. :)
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:32:45 am
Quote
You're using the wrong header, as stated before.
It should be: #include <cmath>, which puts everything into the std namespace.

OMG that was just an example!!!!!!!!!

I meant that I might be including lots of other peoples files which don't use namespaces...
Title: Re: C++: Two functions with same name dilema
Post by: eXpl0it3r on April 09, 2013, 09:36:28 am
OMG that was just an example!!!!!!!!!
OKAY!!!!! ;D

I meant that I might be including lots of other peoples files which don't use namespaces...
Yes, that's possible, that's also why namespaces were invented in the first place. I usually tend to not use libraries that don't make use of namespaces, unless there's no alternative and the names are uniquely enough (i.e. many C libraries use some ugly prefixes, but at least they make the names unique). ;)
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 09:42:21 am
But namespaces don't completely solve the problem. Because if you have tons of lazy programmers all giving you their lib, and they make namespaces like a:: or xyz:: then you're not only going to get function name conflicts but also namespace conflicts...

Anyway thanks everyone for your help. I feel smarter  :-*
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 10:21:45 am
SUCESS!

I actually managed to do it, and it works!?? Pretend that lib1 is MY library, and lib2 is the <math> library.

main.cpp
#include <iostream>

namespace myNamespace2{
        #include "lib2.h"
}

#include "lib1.h"

int main(){
        std::cout << func() << std::endl;
        return 0;
}

lib1.h
#pragma once

int func(){
        return myNamespace2::func();
}

lib2.h
#pragma once

int funcc(){
        return 3;
}

int func(){
        return funcc();
}

Outputs: 3

You all told me it couldn't be done!
Title: Re: C++: Two functions with same name dilema
Post by: Laurent on April 09, 2013, 10:35:29 am
Quote
You all told me it couldn't be done!
I said it would work only if the other library is completely inlined (everything in header, no compiled library). Which is the case here. But is it also the case in your real program?
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 11:00:47 am
Quote
You all told me it couldn't be done!
I said it would work only if the other library is completely inlined (everything in header, no compiled library). Which is the case here. But is it also the case in your real program?

Ah, nevermind, it doesn't work on math.h. Says 'floor is not a member of myNamespace2'

Also, I just realised now that round() isn't even inside <math>  ;D

I definitely need some sleep.

Thanks again everyone..

PS: Does anyone know if it's possible to only load/include a specific function from a file? e.g. Say I only wanted to include the floor() function from <cmath> without loading all the other functions, is that possible?
Title: Re: C++: Two functions with same name dilema
Post by: Laurent on April 09, 2013, 11:56:32 am
Quote
Does anyone know if it's possible to only load/include a specific function from a file? e.g. Say I only wanted to include the floor() function from <cmath> without loading all the other functions, is that possible?
No.
Title: Re: C++: Two functions with same name dilema
Post by: Nexus on April 09, 2013, 12:34:25 pm
Three pages discussion about how to workaround namespaces? tom64, you should really use namespaces the way they are intended to be used. As Laurent already mentioned in the beginning, your code will eventually get more readable if you see the namespaces for each function and class.

Introducing dirty hacks because you don't want to write a few more characters is a very bad idea. Especially since the pure writing usually isn't the limiting factor at programming, and today's IDEs and tools are capable to auto-complete.
Title: Re: C++: Two functions with same name dilema
Post by: tom64 on April 09, 2013, 12:46:07 pm
Quote
Introducing dirty hacks because you don't want to write a few more characters is a very bad idea.

I know, but I'm talking about doing it for very generic/general/basic functions, not everything.
Title: Re: C++: Two functions with same name dilema
Post by: Laurent on April 09, 2013, 12:58:29 pm
Quote
I know, but I'm talking about doing it for very generic/general/basic functions, not everything.
It's still bad. Even if it was for a single function.