Is it possible to reuse variable names using a namespace?

For example, initially I have an example program:

#include<iostream> #include<algorithm> using namespace std; int main() { int a[3]; sort(begin(a),end(a)); cin; } 

Now I want to change std::cin (to provide more functions, for example, calling a function when input fails). Therefore, I present the title of mystd.h as:

 #include<iostream> #include<algorithm> //begin of mystd.h namespace mystd { struct cin_wrapper { }cin; } //end of mystd.h using namespace std; int main() { int a[3]; sort(begin(a),end(a)); mystd::cin; } 

But the change seems uncomfortable. (Users must specify all components using std::sort;using mystd::cin; or replace all cin with mystd::cin . mystd::cin using namespace std;using mystd::cin; causes cin ambiguous)

In fact, I'm going to write a modified standard library and use it as conveniently as the original one. The ideal code I want for users to be able to write is:

(PS: this means that mystd can simply be used as std , and does not mean that I want users to use using namespace everywhere)

 #include<iostream> #include<algorithm> #include "mystd.h" using namespace mystd; int main() { int a[3]; sort(begin(a),end(a));//std::sort cin;//mystd::cin } //or int main() { int a[3]; mystd::sort(mystd::begin(a),mystd::end(a));//sort, begin, end from std mystd::cin; } 

I tried adding using namespace std; in mystd , but also causes ambiguity.

One of the difficult decisions I can make is to create a using statement, such as using std::string; in mystd , for all std members that have not been changed.

Is there a more practical way to implement mystd.h ?

+7
c ++ c ++ 11
source share
3 answers

If you really insist on it, you can do this by presenting your using statements in nested areas. For example:

 using namespace std; int main() { using namespace mystd; int a[3]; sort(begin(a), end(a));//std::sort cin_wrapper w;//mystd::cin } 

Everything related to using namespace std; should be avoided though (using other, more limited namespaces is not so bad, but it's a huge truck with worms of worms that you open).

+1
source share

This is not a good idea, because it is very fragile.

Imagine someone writing your β€œperfect” code. Then, one fine day, you write mystd::sort , which takes a range instead of two iterators.

Suddenly, the value of the existing code changed unexpectedly, and it starts to fail to compile, because it did not expect that now the number of parameters should be one, not two.

0
source share

Your requirement is smoothed out with the invention of the "namespace".

If "productB" is your product with the same names as in "productA", you want to rewrite it, and then you decide which names your users will use through some of the "used" operators in your interface file "productB.h "".

Source file productA.h:

 namespace productA { void f1(); void f2(); } 

your original productB.h file: here you decide what to use:

 namespace productB { void f1(); void f2(); } using productA::f1; using productB::f2; 

implementation:

 #include <iostream> // std::cout #include "productA.h" #include "productB.h" void productA::f1() { std::cout << "called A::f1" <<std::endl; } void productA::f2() { std::cout << "called A::f2" <<std::endl; } void productB::f1() { std::cout << "called B::f1" <<std::endl; } void productB::f2() { std::cout << "called B::f2" <<std::endl; } 

application: very convenient

 #include "productA.h" #include "productB.h" int main () { f1(); f2(); } 

exit:

 called A::f1 called B::f2 

notice: nothing is clear

0
source share

All Articles