Should it be hard to overload functions in namespaces?

I wrote code with this structure

namespace util { void read (int &); template <typename T> void read (T &); } void foo (); using namespace util; namespace { // A void read (MyType &, int); void do_something () { MyType t; int i; // using util::read; // B read (i); // C read (t,i); // D } } void foo () { do_something (); } 

In the first line, C did not compile unless I completely qualify it as util::read(i) or the uncommented line B, but this causes line D to crash.

Specialization of using the :: read template is not possible, because the number of arguments is different (up to C ++ 0x).

Including A string in namespace util not an option because I don't want to export a new read .

I could rename read(MyType&,int) , but that violates the ahm style.

Is there a way to make these cross-overloads useful? Is there a good reason they shouldn't?

+4
source share
2 answers

Yes. This is hard. This is actually impossible.

The best you can do is hide the names. You are right to use using (B) to get around the complications that the name hides introduces into the mix when you also overload functions - D should still function in this case. Please show the error you received for it.

+3
source

Different namespaces mean different full names of identifiers contained in them, so they are not overloads of each other (at least for the Microsoft compiler and GCC - I'm not sure how the standard is defined).

Try the following:

 namespace util { void read (int &); template <typename T> void read (T &); } void foo (); namespace { // A using ::util::read; void read (MyType &, int); void do_something () { MyType t; int i; // using util::read; // B read (i); // C read (t,i); // D } } void foo () { do_something (); } 
0
source

Source: https://habr.com/ru/post/1347924/


All Articles