473,395 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 4536
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. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.