473,473 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.

Jul 22 '05 #1
6 1935

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

[snip]

#include <iostream> // for std::cout
#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;
std::cout << ""my custom math function\n" << std::endl;

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

}
}

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;
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.

return 0;
}


-Sharad
Jul 22 '05 #2

"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
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>
Why are you including this header? You're not referring to
anything declared by it.
namespace myMath {

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

}

in my .cpp file

#include "my.h"
If you want to call 'std::math' from this translation unit,
#include <cmath>

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
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);

}
}

using namespace myMath; // hopefully this will cause my custom sin function to be called
It will, if it has been linked with the rest of your program.


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

return 0;
}
So, what am I doing wrong here?


See above.

-Mike
Jul 22 '05 #3
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.
Jul 22 '05 #4
you may track the bug (not yet confirmed) at
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16668
Jul 22 '05 #5
johny smith posted:

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


std::sin(input);

For the global namespace:

::sin(input);
-JKop
Jul 22 '05 #6
"johny smith" <pr**************@charter.net> wrote:
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
}


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).
Jul 22 '05 #7

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

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
25
by: kj | last post by:
Consider the following XML document: <?xml version='1.0' encoding='UTF-8'?> <bar:foo xmlns:bar='someuri'> <baz/> </bar:foo> What namespace does baz belong to? What is this namespace bound...
4
by: BH | last post by:
Hi C# & .NET detectives, can you help with two problems I've encountered lately? 1) These seem equivalent to me but one fails and the other doesn't. This fails with "type or namespace...
29
by: Tiraman | last post by:
Hi, I Build my own dll with my own namespace name and i would like to put it in one place but for the project bin folder so all of the projects will be able to use it . i tried to put the dll...
4
by: Kevin Newman | last post by:
The primary problem I've had with php is the lack of namespaces, which makes OOP very difficult to organize, since you end up with large number of classes cluttering up the same namespace - which...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
7
by: Techno_Dex | last post by:
Is there a way to configure a WebService Namespace in a config file that can be set (i.e. to )? I have a webservice that I might need to configure/move on multiple different servers but from...
10
by: Andy Fish | last post by:
hi, I have an XSLT which is producing XML output. many of the nodes in the output tree contain namespace declarations for namespaces that are used in the source document even though they are...
3
by: =?Utf-8?B?QWxleGFuZGVyIFd5a2Vs?= | last post by:
I recently raninto major problems when I added the Exception namespace to the Project which has my DBML file attached to it. Once I renamed all the Exceptions instances to Syste.Exception the...
17
by: Peng Yu | last post by:
Hi, I'm wondering if there is something in namespace like the 'private' keyword in class? I want to define some class or function that can only be used within that namespace. Thanks, Peng
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.