472,372 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,372 software developers and data experts.

Assigning std::sqrt to function pointer

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.
Jul 22 '05 #1
6 4466
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
Jul 22 '05 #2
"Prune Tracy" <tr********@hotmail.com> 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.
Jul 22 '05 #3
"Ioannis Vranos" <iv*@guesswh.at.grad.com> 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
Jul 22 '05 #4
Siemel Naran wrote:
"Prune Tracy" <tr********@hotmail.com> 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++.

Jul 22 '05 #5
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:ck*************@news.t-online.com...
Siemel Naran wrote:
"Prune Tracy" <tr********@hotmail.com> 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
Jul 22 '05 #6
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
Jul 22 '05 #7

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

Similar topics

10
by: JKop | last post by:
An excerpt from the Standard: 4 Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any...
10
by: pauldepstein | last post by:
I am writing a program which will use the ceiling and floor of square roots. If a positive integer is an exact square (and is not overly huge), does sqrt return an integer-valued real? Or...
2
by: Jose Suero | last post by:
Hi all I have a dynamically created button, I can add an event handler with: AddHandler button.click, AddressOf static_function This works great, but what I need is to create a function that...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
1
by: Pankaj | last post by:
Hello friends, I am implementing something like PM |------------------------------------------------------ | | | | | ------- -|...
7
by: Lighter | last post by:
Is overriding a function of a library in accordance with C++ standard? The following code are passed by the VS 2005 and Dev C++. #include <cstdlib> #include <iostream> using namespace std;...
0
by: willievdm | last post by:
Hi all I'm working with Boost.Python at the moment and have to do the following: -Using Boost, assign a char* in C++ to a PySwigObject (SWIG exposed object) of type uchar*, which is received in...
19
by: Gerry Ford | last post by:
I've been pecking away at writing a program that will calculate the inner product of two double-width four-vectors. Larry thinks I'm well started with the following source to populate a vector:...
2
by: Adam Sandler | last post by:
Hello, Is it possible to assign a function key to a toolstripbutton? I don't see the property for such an action in the properties for the tsButton. I have a "new" button on the toolstrip. ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.