namespace question/problem

Discussion in 'C++' started by johny smith, Jul 22, 2004.

  1. johny smith

    johny smith Guest

    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.
    johny smith, Jul 22, 2004
    #1
    1. Advertising

  2. johny smith

    Sharad Kala Guest

    "johny smith" <> wrote in message
    news:...

    [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
    Sharad Kala, Jul 22, 2004
    #2
    1. Advertising

  3. johny smith

    Mike Wahler Guest

    "johny smith" <> wrote in message
    news:...
    > 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
    Mike Wahler, Jul 22, 2004
    #3
  4. johny smith

    poiuz24 Guest

    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.
    poiuz24, Jul 22, 2004
    #4
  5. johny smith

    poiuz24 Guest

    poiuz24, Jul 22, 2004
    #5
  6. johny smith

    JKop Guest

    johny smith posted:


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


    std::sin(input);

    For the global namespace:

    ::sin(input);


    -JKop
    JKop, Jul 22, 2004
    #6
  7. johny smith

    Old Wolf Guest

    "johny smith" <> 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).
    Old Wolf, Jul 22, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    563
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,125
  3. Anonymous
    Replies:
    3
    Views:
    529
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    448
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,351
    Howard
    Nov 29, 2004
Loading...

Share This Page