Assigning std::sqrt to function pointer

Discussion in 'C++' started by Prune Tracy, Oct 14, 2004.

  1. Prune Tracy

    Prune Tracy Guest

    Hi, I can't get this seemingly harmless code to compile with Comeau online.

    #include <cmath>

    typedef float (*float_func)(float);
    typedef double (*double_func)(double);

    int main() {
    float_func f = std::sqrt; // OK
    double_func d = std::sqrt; // line 8: error
    }

    Comeau says:
    line 8: error: no instance of overloaded function "std::sqrt"
    matches the required type

    It works fine on gcc 3.2.3. Is gcc wrong to accept it?

    What is the cause of this?

    Thanks.
    Prune Tracy, Oct 14, 2004
    #1
    1. Advertising

  2. Prune Tracy wrote:
    > Hi, I can't get this seemingly harmless code to compile with Comeau online.
    >
    > #include <cmath>
    >
    > typedef float (*float_func)(float);
    > typedef double (*double_func)(double);
    >
    > int main() {
    > float_func f = std::sqrt; // OK
    > double_func d = std::sqrt; // line 8: error
    > }
    >
    > Comeau says:
    > line 8: error: no instance of overloaded function "std::sqrt"
    > matches the required type
    >
    > It works fine on gcc 3.2.3. Is gcc wrong to accept it?



    The standard library has only one version of sqrt, the double sqrt(double);


    So your float version is not portable, and the double version should
    compile, which it doesn't in "strict mode" in that "web compiler" - but
    it does in relaxed mode.

    It looks like a compiler (but probably web) bug.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Oct 14, 2004
    #2
    1. Advertising

  3. Prune Tracy

    Siemel Naran Guest

    "Prune Tracy" <> wrote in message

    > Hi, I can't get this seemingly harmless code to compile with Comeau

    online.
    >
    > #include <cmath>
    >
    > typedef float (*float_func)(float);
    > typedef double (*double_func)(double);
    >
    > int main() {
    > float_func f = std::sqrt; // OK
    > double_func d = std::sqrt; // line 8: error
    > }
    >
    > Comeau says:
    > line 8: error: no instance of overloaded function "std::sqrt"
    > matches the required type
    >
    > It works fine on gcc 3.2.3. Is gcc wrong to accept it?
    >
    > What is the cause of this?


    It compiles fine for me on Borland C++ 6. Strangely, if I include <math.h>
    instead, I get an error that we can't convert double (*)(double) to float
    (*)(float), which is line 7 in your program.
    Siemel Naran, Oct 14, 2004
    #3
  4. Prune Tracy

    P.J. Plauger Guest

    "Ioannis Vranos" <> wrote in message
    news:1097735468.982319@athnrd02...

    > Prune Tracy wrote:
    >> Hi, I can't get this seemingly harmless code to compile with Comeau
    >> online.
    >>
    >> #include <cmath>
    >>
    >> typedef float (*float_func)(float);
    >> typedef double (*double_func)(double);
    >>
    >> int main() {
    >> float_func f = std::sqrt; // OK
    >> double_func d = std::sqrt; // line 8: error
    >> }
    >>
    >> Comeau says:
    >> line 8: error: no instance of overloaded function "std::sqrt"
    >> matches the required type
    >>
    >> It works fine on gcc 3.2.3. Is gcc wrong to accept it?

    >
    >
    > The standard library has only one version of sqrt, the double
    > sqrt(double);


    Nope. It has three for the floating-point types, at least three for the
    complex types, and a template version for valarray<T>.

    > So your float version is not portable, and the double version should
    > compile, which it doesn't in "strict mode" in that "web compiler" - but it
    > does in relaxed mode.
    >
    > It looks like a compiler (but probably web) bug.


    I notice that the EDG compiler available at our Exam page:

    http://www.dinkumware.com/exam/dinkumExam.aspx

    also produces this diagnostic, but the later version I'm
    using on my laptop just fine compiles it as expected. Looks
    like a bug that's been fixed somewhere along the line.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
    P.J. Plauger, Oct 14, 2004
    #4
  5. Prune Tracy

    Rolf Magnus Guest

    Siemel Naran wrote:

    > "Prune Tracy" <> wrote in message
    >
    >> Hi, I can't get this seemingly harmless code to compile with Comeau

    > online.
    >>
    >> #include <cmath>
    >>
    >> typedef float (*float_func)(float);
    >> typedef double (*double_func)(double);
    >>
    >> int main() {
    >> float_func f = std::sqrt; // OK
    >> double_func d = std::sqrt; // line 8: error
    >> }
    >>
    >> Comeau says:
    >> line 8: error: no instance of overloaded function "std::sqrt"
    >> matches the required type
    >>
    >> It works fine on gcc 3.2.3. Is gcc wrong to accept it?
    >>
    >> What is the cause of this?

    >
    > It compiles fine for me on Borland C++ 6. Strangely, if I include
    > <math.h> instead, I get an error that we can't convert double (*)(double)
    > to float (*)(float), which is line 7 in your program.


    This is because math.h is the C header, and C doesn't support overloading.
    Therefore, it has only one sqrt function. However, I'm not sure whether
    math.h is supposed to provide the overloads when used in C++.
    Rolf Magnus, Oct 14, 2004
    #5
  6. Prune Tracy

    P.J. Plauger Guest

    "Rolf Magnus" <> wrote in message
    news:cklnk7$7qv$05$-online.com...

    > Siemel Naran wrote:
    >
    >> "Prune Tracy" <> wrote in message
    >>
    >>> Hi, I can't get this seemingly harmless code to compile with Comeau

    >> online.
    >>>
    >>> #include <cmath>
    >>>
    >>> typedef float (*float_func)(float);
    >>> typedef double (*double_func)(double);
    >>>
    >>> int main() {
    >>> float_func f = std::sqrt; // OK
    >>> double_func d = std::sqrt; // line 8: error
    >>> }
    >>>
    >>> Comeau says:
    >>> line 8: error: no instance of overloaded function "std::sqrt"
    >>> matches the required type
    >>>
    >>> It works fine on gcc 3.2.3. Is gcc wrong to accept it?
    >>>
    >>> What is the cause of this?

    >>
    >> It compiles fine for me on Borland C++ 6. Strangely, if I include
    >> <math.h> instead, I get an error that we can't convert double (*)(double)
    >> to float (*)(float), which is line 7 in your program.

    >
    > This is because math.h is the C header, and C doesn't support overloading.
    > Therefore, it has only one sqrt function. However, I'm not sure whether
    > math.h is supposed to provide the overloads when used in C++.


    It is.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
    P.J. Plauger, Oct 14, 2004
    #6
  7. P.J. Plauger wrote:

    > Nope. It has three for the floating-point types, at least three for the
    > complex types, and a template version for valarray<T>.



    Oh well, yes, you are right. The standard says:

    "In addition to the double versions of the math functions in <cmath>,
    C++ adds float and long double overloaded versions of these functions,
    with the same semantics."


    I was not suspecting that C++ provides something more than C90 in this area.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Oct 14, 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. John

    How to use sqrt() function with gcc?

    John, Aug 4, 2006, in forum: C Programming
    Replies:
    3
    Views:
    939
    Keith Thompson
    Aug 4, 2006
  2. Replies:
    10
    Views:
    651
    Victor Bazarov
    Oct 22, 2005
  3. Ramesh
    Replies:
    11
    Views:
    4,179
    James Kanze
    Dec 27, 2008
  4. Alf P. Steinbach
    Replies:
    10
    Views:
    3,046
    Alf P. Steinbach
    Jul 27, 2011
  5. weston
    Replies:
    1
    Views:
    238
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page