473,554 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)(f loat);
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 4543
Prune Tracy wrote:
Hi, I can't get this seemingly harmless code to compile with Comeau online.

#include <cmath>

typedef float (*float_func)(f loat);
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********@hot mail.com> wrote in message
Hi, I can't get this seemingly harmless code to compile with Comeau online.
#include <cmath>

typedef float (*float_func)(f loat);
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@athnrd0 2...
Prune Tracy wrote:
Hi, I can't get this seemingly harmless code to compile with Comeau
online.

#include <cmath>

typedef float (*float_func)(f loat);
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********@hot mail.com> wrote in message
Hi, I can't get this seemingly harmless code to compile with Comeau

online.

#include <cmath>

typedef float (*float_func)(f loat);
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********@hot mail.com> wrote in message
Hi, I can't get this seemingly harmless code to compile with Comeau

online.

#include <cmath>

typedef float (*float_func)(f loat);
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
5043
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 objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration,...
10
7305
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 might the sqrt function tell me that, for example, sqrt(1024) = 31.99999999999999999999999 You might say "try it". The problem is that I can't try...
2
3721
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 takes the control, the event and the function as parameters, something like: function addevent(control as object, event as string, functionname as...
8
2461
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 in the following way void (*f)(int);
1
2870
by: Pankaj | last post by:
Hello friends, I am implementing something like PM |------------------------------------------------------ | | | | | ------- -| send_if( data) | | | M | | | | | |
7
6618
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; size_t strlen(const char* p)
0
1345
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 C++ as a boost::python::object class. The uchar* pointer in the PySwigObject points to a buffer containing image data. All I wish to do is...
19
2431
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: #include <vector> #include <algorithm> #include <iterator> #include <iostream> #include <math.h>
2
9187
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. I'd like F2 to fire the click event handler. Is this possible? Thanks!
0
7615
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7539
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7819
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7581
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5446
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3589
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.