Assigning std::sqrt to function pointer

P

Prune Tracy

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

Ioannis Vranos

Prune said:
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.
 
S

Siemel Naran

Prune Tracy said:
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.
 
P

P.J. Plauger

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 said:
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
 
R

Rolf Magnus

Siemel said:
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++.
 
I

Ioannis Vranos

P.J. Plauger said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top