Connecting Tech Pros Worldwide Help | Site Map

namespace question/problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:44 PM
johny smith
Guest
 
Posts: n/a
Default namespace question/problem

I was defining one of my own math functions for sin. So I thought I would
create a unique name space. Then use that namespace in my program to call
my custom sin math function. That way I was hoping that I would not call
the math library math function but my own custom.

But it did not seem to work. Here is what I did.


in my.h file

#include <cmath>
namespace myMath {

double sin( double ); // this is a declaration for my custom function

}

in my .cpp file

#include "my.h"

namespace myMath {

double sin( double input ) // this is my custom math function
definition
{
stdio::cout << "my custom math function\n" << std::endl;

sin( input ); // this a call to the <cmath> library

}
}

using namespace myMath; // hopefully this will cause my custom sin function
to be called

int main()
{
// I was hoping this would call my math function sin not the <cmath>
function since I am using the myMath namespace.
std::cout << "the value is " << sin(.3) << std::endl;

return 0;
}


So, what am I doing wrong here?

Many thanks.




  #2  
Old July 22nd, 2005, 04:44 PM
Sharad Kala
Guest
 
Posts: n/a
Default Re: namespace question/problem


"johny smith" <princetonharvard@charter.net> wrote in message
news:10fubd5kqg07b1b@corp.supernews.com...

[snip]

#include <iostream> // for std::cout
[color=blue]
> #include "my.h"
>
> namespace myMath {
>
> double sin( double input ) // this is my custom math function
> definition
> {
> stdio::cout << "my custom math function\n" << std::endl;[/color]

std::cout << ""my custom math function\n" << std::endl;

[color=blue]
> sin( input ); // this a call to the <cmath> library[/color]

return std::sin(input);

[color=blue]
> }
> }
>
> using namespace myMath; // hopefully this will cause my custom sin[/color]
function[color=blue]
> to be called
>
> int main()
> {
> // I was hoping this would call my math function sin not the <cmath>
> function since I am using the myMath namespace.
> std::cout << "the value is " << sin(.3) << std::endl;[/color]

std::cout << "the value is " << myMath::sin(.3) << std::endl;

Somehow VC7 and g++ 3.3.1 find call to sin function ambiguous, Comeau finds
it ok though. So the way I have done is to explicitly mention which sin
function I want to be called here.

[color=blue]
> return 0;
> }[/color]

-Sharad


  #3  
Old July 22nd, 2005, 04:44 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: namespace question/problem


"johny smith" <princetonharvard@charter.net> wrote in message
news:10fubd5kqg07b1b@corp.supernews.com...[color=blue]
> I was defining one of my own math functions for sin. So I thought I would
> create a unique name space. Then use that namespace in my program to call
> my custom sin math function. That way I was hoping that I would not call
> the math library math function but my own custom.
>
> But it did not seem to work. Here is what I did.
>
>
> in my.h file
>
> #include <cmath>[/color]

Why are you including this header? You're not referring to
anything declared by it.
[color=blue]
> namespace myMath {
>
> double sin( double ); // this is a declaration for my custom function
>
> }
>
> in my .cpp file
>
> #include "my.h"[/color]

If you want to call 'std::math' from this translation unit,
#include <cmath>
[color=blue]
>
> namespace myMath {
>
> double sin( double input ) // this is my custom math function
> definition
> {
> stdio::cout << "my custom math function\n" << std::endl;
>
> sin( input ); // this a call to the <cmath> library[/color]

No, it's not. It's a recursive invocation of 'myMath::sin()'
If you have <cmath> #included, you can call the standard 'sin()'
function with:

::std::sin(input);
[color=blue]
>
> }
> }
>
> using namespace myMath; // hopefully this will cause my custom sin[/color]
function[color=blue]
> to be called[/color]

It will, if it has been linked with the rest of your program.

[color=blue]
>
> int main()
> {
> // I was hoping this would call my math function sin not the <cmath>
> function since I am using the myMath namespace.[/color]

Yes, it should.
[color=blue]
> std::cout << "the value is " << sin(.3) << std::endl;
>
> return 0;
> }
>
>
> So, what am I doing wrong here?[/color]

See above.

-Mike


  #4  
Old July 22nd, 2005, 04:45 PM
poiuz24
Guest
 
Posts: n/a
Default Re: namespace question/problem

i suspect you use GCC since i can reproduce the problem.
this one works as you like:

------------------------------------
#include <iostream>
#include <cmath>

namespace myMath {

float sin( float input ) // this is my custom math function
{
std::cout << "my custom math function\n" << std::endl;
return std::sin( input ); // this a call to the <cmath> library
}
}

using namespace myMath; // hopefully this will cause my custom sin function

int main()
{
std::cout << "the value is " << sin(.3F) << std::endl;
return 0;
}
------------------------------------

you'll note some stuff: first use of "float" instead of "double"
(why? see below). 2nd: you must qualify the call to the standard
sin wihtin your custom sin since otherwise you'll get into an
infinite loop, since the just declared custom sin is visible
within the scope of the function body. this is expected behaviour.
a declaration is immediately visible ..

also, to force call to the float version, i added "F" to the literal.

now the interesting part: why does it not work with double?
(it will also work with long double btw).

IMHO it's a bug in "cmath" in GCC.

from "cmath":
--------------------------------------------------
#include <math.h>

namespace std {
....
using ::sin;

inline float
sin(float __x)
{ return __builtin_sinf(__x); }

inline long double
sin(long double __x)
{ return __builtin_sinl(__x); }
....
}
--------------------------------------------------

you see, "using ::sin" just drags in the ::sin from math.h
into namespace std.

well, thats ok, since sin(double) should reside in std.

but the "using ::sin" does not remove ::sin from ::
thats the bug

ugh.
  #5  
Old July 22nd, 2005, 04:45 PM
poiuz24
Guest
 
Posts: n/a
Default Re: namespace question/problem

you may track the bug (not yet confirmed) at
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16668
  #6  
Old July 22nd, 2005, 04:45 PM
JKop
Guest
 
Posts: n/a
Default Re: namespace question/problem

johny smith posted:

[color=blue]
> sin( input ); // this a call to the <cmath> library[/color]

std::sin(input);

For the global namespace:

::sin(input);


-JKop
  #7  
Old July 22nd, 2005, 04:46 PM
Old Wolf
Guest
 
Posts: n/a
Default Re: namespace question/problem

"johny smith" <princetonharvard@charter.net> wrote:
[color=blue]
> I was defining one of my own math functions for sin. So I thought I would
> create a unique name space. Then use that namespace in my program to call
> my custom sin math function. That way I was hoping that I would not call
> the math library math function but my own custom.
>
> But it did not seem to work. Here is what I did.
> namespace myMath {
> double sin( double ); // this is a declaration for my custom function
> }[/color]

I don't think you can do this. The names of C library functions
are all reserved for the implementation (for example, they could
be macros). (I'd be grateful if someone could clarify whether this
applies to names with external linkage but within user-defined namespaces).
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.