-
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
-
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);
}
-
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".
-
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.
-
What is the name of the function, and in which library is it?
I'm just asking from a general point of view.
You should really use a namespace for your own 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.
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++.
-
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 ;)
-
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?
-
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.
-
So you would prefer to type:
xaxis = hsuxz::round(i)+hsuxz::otherFunc();
than
xaxis = round(i)+otherFunc();
??
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...
-
So you would prefer to type:
Yes.
-
So you would prefer to type:
Yes.
So you would prefer:
xaxis = TankHeroMemberLibraryIsAwsome::round().. ;D
-
TankHeroMemberLibraryIsAwsome
No, TankHeroMemberLibraryIsAwesome. Or just tank:: or thl:: or hero::. ;)
-
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....
-
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...
-
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;
}
-
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;
}
-
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. ;)
-
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...???
-
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::
-
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.
-
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.
-
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.. :'(
-
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
-
That's why I'm using that as an example.
But those are C era functions, it's a bad example.
-
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 ;)
-
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?
-
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).
-
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... :-\
-
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. :) :'(
-
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. :)
-
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...
-
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). ;)
-
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 :-*
-
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!
-
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?
-
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?
-
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.
-
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.
-
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.
-
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.