473,698 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

namespaces and function overloading

Hi all,

I have the following code:

namespace A {
inline void func(int) { ...; }
inline void func(float) { ...; }
inline void func(char) { ...; }
}

namespace B {
inline void func(double) { ...; }

inline myApp()
{
int a;
func(a); // error here
}
}

Which results in the compile-error:
"cannot convert parameter 1 from 'int' to 'double' ...

Which I guess means that all the definitions of func in namespace A are
forgotten in namespace B. Is this something general for c++ or is it
specific for my microsoft visual c++ 6.0 . And more importantly, is there a
work-around / solution ?
thanks ahead,
Richard
Jul 23 '05 #1
12 2969
RA Scheltema wrote:

Hi all,

I have the following code:

namespace A {
inline void func(int) { ...; }
inline void func(float) { ...; }
inline void func(char) { ...; }
}

namespace B {
inline void func(double) { ...; }

inline myApp()
{
int a;
func(a); // error here
}
}

Which results in the compile-error:
"cannot convert parameter 1 from 'int' to 'double' ...
Hmm. I don't get this error when compiling the above. And I
use VC++ 6.0

Which I guess means that all the definitions of func in namespace A are
forgotten in namespace B.


No, they are not forgotten. But namespace A is not searched for the
functions

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #2
> Hmm. I don't get this error when compiling the above. And I
use VC++ 6.0
Hmm, in my project its part of quite a large code-base. Maybe I should have
checked the code :(, however the example is a very stripped down version of
my problem.
No, they are not forgotten. But namespace A is not searched for the
functions


But this means that when a function "func" is found in namespace B it will
only try this one and don't bother with the ones in namespace A ? This would
concur with what I'm finding in my project, since the error does not occur
when I don't define a function "func" in namespace B (in my project that is
....).

I did find the following solution:

using A::func;
namespace B{
....
}
however this results in the error:
"ambiguous call to func ..."

and explicit casting does not solve the problem.
Is there something I'm doing wrong ?
regards,
Richard
Jul 23 '05 #3
>> Hmm. I don't get this error when ?
compiling the above. And I
use VC++ 6.0
Hmm, in my project its part of quite a
large code-base. Maybe I should have
checked the code :(, however the example
is a very stripped down version of
my problem.
What Karl meant is that conversion from int
to double is legal and you shouldn't have
had an error about it. It may be that your
error was different on the real code, I
think we all understand what you mean.
No, they are not forgotten. But namespace
A is not searched for the functions But this means that when a function
"func" is found in namespace B it will
only try this one and don't bother with
the ones in namespace A ? This would
concur with what I'm finding in my
project, since the error does not occur
when I don't define a function "func" in
namespace B (in my project that is
...).
This:

namespace A
{
void f();
}

namespace B
{
void g()
{
f(); // error
}
}

is invalid. Namespaces are meant to
prevent name clashing and therefore, it is
by design that function in a namespace are
not available from another, without special
syntax.
I did find the following solution: using A::func;
namespace B{
...

}
This is one way of doing it.
however this results in the error:
"ambiguous call to func ..."
Where? If it's at namespace scope, this is
weird because in a namespace, local names
will always be preferred to other names.
Therefore, this is valid:

namespace A
{
void f(int) {}
}

using namespace A;
namespace B
{
void f(int) {}

void g()
{
f(10); // ok, calls B::f()
}
}

The problem is in the global scope:

int main()
{
using namespace A;
using namespace B;

f(10); // ambiguous
}
and explicit casting does not solve the
problem.
What casting?
Is there something I'm doing wrong ?


You don't understand namespaces. Read
about them.
Jonathan

Jul 23 '05 #4

"Jonathan Mcdougall"
Where? If it's at namespace scope, this is
weird because in a namespace, local names
will always be preferred to other names.


I find with BCB6 that A::func(double) is called with this test:
namespace A {
inline void func(int) { ; }
inline void func(float) { ; }
inline void func(char) { ; }
inline void func(double) { ; }
}

namespace B {
inline void func(double) { ; }

inline void myApp()
{
using A::func;
//using B::func;
double a;
func(a); // error here
}
}

#pragma argsused
int main(int argc, char* argv[])
{ B::myApp();
return 0;
}
Jul 23 '05 #5
>> Where? If it's at namespace scope, this is
weird because in a namespace, local names
will always be preferred to other names.
I find with BCB6 that A::func(double) is called with this test:


That's not the same thing.
namespace A {
inline void func(int) { ; }
inline void func(float) { ; }
inline void func(char) { ; }
inline void func(double) { ; }
}

namespace B {
inline void func(double) { ; }

inline void myApp()
{
using A::func;
//using B::func;
double a;
func(a); // error here
This should prefer A::func to B::func because you specificaly say it
with 'using A::func'. You should have no error because there is no
ambiguity.

namespace A
{
void f(int);
}

namespace B
{
void f(int);

void g()
{
f(10); // calls B::f

using namespace A;
f(10); // calls B::f (preferred over A::f)

using A::f;
f(10); // calls A::f (because of using)
}
}

int main()
{
f(10); // error, not found

// dump both namespaces here
using namespace A;
using namespace B;

// ambiguous (using both A::f and B::f)
f(10);

// specifically use A::f
using A::f;

// calls A::f
f(10);

// specifically use B::f (this does *not* override
// the previous using declaration)
using B::f;

// ambiguous (using both A::f and B::f)
f(10);
}
}

}

#pragma argsused
Make sure you strip non standard code before posting.
int main(int argc, char* argv[])
{ B::myApp();
return 0;
}


--
Jonathan
[comp.lang.c++ FAQ] - http://www.parashift.com/c++-faq-lite/

Jul 23 '05 #6
If I want to prefer a swap function written for a class in its namespace
over std:swap I thought I should have a using declaration for std:swap
before the call. So that won't work as I expected.

Fraser.
Jul 23 '05 #7
Instead of a using declaration I want a using directive with minimal code
blocked. I.E:
{
using namespace std;
swap(a1,a2);
}
Argument dependent lookup will link the call to any swap function in the
namespace of the type of a1/a2.

Fraser.

If I want to prefer a swap function written for a class in its namespace
over std:swap I thought I should have a using declaration for std:swap
before the call. So that won't work as I expected.

Fraser.

Jul 23 '05 #8
> If I want to prefer a swap function
written for a class in its namespace
over std:swap
You should have no problem if no
using directive is used:

# include <algorithm>

namespace N
{
class A {};
void swap(const A &a, const A &b);
}

int main()
{
N::A a, b;
swap(a, b); // A::swap()
}

Since ADL is used as a "last resort",
the compiler will use another swap
function if it can be found (such as
with a using directive/declaration or
with a global swap function).

Be careful though: Microsoft Visual
C++ 7.0 does not handle ADL
correctly since it ignores it for
ordinary functions (as opposed to
operators). Therefore, the above
example does not compile. I do not know
for Visual C++ 7.1.
I thought I should have a using
declaration for std:swap before
the call. So that won't work as I
expected.


I don't understand that.

Maybe if you provided some actual
code we could help you more with
your problem than with theoritical
solutions.

Jonathan

Jul 23 '05 #9

"Jonathan Mcdougall"
If I want to prefer a swap function
written for a class in its namespace
over std:swap
You should have no problem if no
using directive is used:

# include <algorithm>

namespace N
{
class A {};
void swap(const A &a, const A &b);
}

int main()
{
N::A a, b;
swap(a, b); // A::swap()


Don't you mean N::swap()?

}

Since ADL is used as a "last resort",
the compiler will use another swap
function if it can be found (such as
with a using directive/declaration or
with a global swap function).


You said in the comment N::swap will be called. My compiler chooses
N::swap. It is the behaviour I want. I'd like to know where in the
standard is the rule for this.

In general ADL is not any kind of last resort. It leads to ambiguity.

Fraser.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1660
by: Frederick Reeve | last post by:
Hey all I have a little compilation problem I think is related to name spaces but being new to C++ still I don't know the program (http://rafb.net/paste/results/f1416037.html) when it is being linked returns with "undefined reference to foo" for function in a C lib i have linked... any Ideas? I am attempting to link a C lib and I think the complication comes in here also. If it would be better I can post the program in another...
6
1631
by: BigMan | last post by:
I've come to the decision to use multiple namespaces for a very specific reason: I like to use descriptive names of UDTs and I sometimes come up with long UDT names (and file names too, since I name a source file after the UDT that is defined in it, if any). I've also noticed, however, that certain types are related more than logically but also lexically - they start with the same word (which happens to be the basic concept that they...
1
1170
by: Talin | last post by:
Thanks to everyone for pointing out my bone-headed errors :) I don't *usually* make that sort of mistake... What I'm attempting to do is create an implentation of the unification algorithm. Now, its fairly straightforward to do unification on my own custom types, all I have to do is define a unify() method for each class: def unify( self, other, bindings ): ...
39
2192
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know your arguments too. Thanks,
45
3278
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
2
1863
by: Beeeeeeeeeeeeves | last post by:
Does C# really HAVE to have namespaces? Isn't there any way I can turn them off? I did a bit of dabbling in VB.NET but have decided not to switch to it permanantly as I've got too much pre-written generic code in C# and the cast operator overloading thing but one of the things I like about VB.NET is it doesn't enthrust the pointless idea of namespaces on you, even though I notice that you can have them if you want. Are they turn-off-able?
11
1775
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I understand that you declare your intention to use a namespace within a page through the "Inherits" attribute. I know that using "Inherits" isn't absolutely necessary, it's just recommended so the developer doesn't have to type out the entire...
1
4242
by: thetrueaplus | last post by:
Hey all. I posted this originally: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/b1e4b76201ce17f1 I got the enums to work how I like but I am having a problem with the overloading of the assignment operator: namespace UserAffilliationType {
11
3810
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char & __cdecl graph::operator<<(class std::basic_ostream<char,struct std::char_traits<char &,class graph::Node &)" (??6graph@@YAAAV?...
0
9156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2323
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.