Conversion function problems

D

Dave

Hello all,

Can anybody help with the problem below? I'm trying to define a conversion
function that converts objects to function pointers and am getting a compile
error on the line indicated below. The compiler interprets this is a
function returning a function, which, of course is illegal... What is the
correct syntax to accomplish this?

Thanks,
Dave


#include <iostream>

using namespace std;

double foo_func(int n) {return n + 1.5;}

class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error here!
};

int main()
{
foo_t f;

cout << f(12) << endl; // Expecting "13.5"...

return 0;
}
 
J

Josephine Schafer

Dave said:
Hello all,

Can anybody help with the problem below? I'm trying to define a conversion
function that converts objects to function pointers and am getting a compile
error on the line indicated below. The compiler interprets this is a
function returning a function, which, of course is illegal... What is the
correct syntax to accomplish this?

Thanks,
Dave


#include <iostream>

using namespace std;

double foo_func(int n) {return n + 1.5;}

class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error here!
};

int main()
{
foo_t f;

cout << f(12) << endl; // Expecting "13.5"...

return 0;
}

Try this -
#include <iostream>

using namespace std;

double foo_func(int n) {return n + 1.5;}
typedef double (*fp)(int) ;
class foo_t
{
public:
operator fp(){return foo_func;}
};

int main()
{
foo_t f;

cout << f(12) << endl; // Expecting "13.5"...

return 0;
}

HTH,
J.Schafer
 
D

David White

Dave said:
Hello all,

Can anybody help with the problem below? I'm trying to define a conversion
function that converts objects to function pointers and am getting a compile
error on the line indicated below. The compiler interprets this is a
function returning a function, which, of course is illegal... What is the
correct syntax to accomplish this?

Thanks,
Dave


#include <iostream>

using namespace std;

double foo_func(int n) {return n + 1.5;}

class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error here!

operator double (*())(int n) { return foo_func;}

DW
 
D

David White

David White said:
here!

operator double (*())(int n) { return foo_func;}

Sorry, my compiler is playing tricks on me. I'm sure that compiled without
error once, but it won't do it now. I don't know why. However, this works:

double foo_func(int n) {return n + 1.5;}

typedef double (*ff)(int n);

class foo_t
{
public:
operator ff() { return foo_func;}
};

DW
 
D

Dave

Josephine Schafer said:
Try this -
#include <iostream>

using namespace std;

double foo_func(int n) {return n + 1.5;}
typedef double (*fp)(int) ;
class foo_t
{
public:
operator fp(){return foo_func;}
};

int main()
{
foo_t f;

cout << f(12) << endl; // Expecting "13.5"...

return 0;
}

HTH,
J.Schafer

Thx!!! Just out of curiosity, is it possible to do this without a typedef???
I've tried a bunch of different ideas, but none work. Is it syntactically
possible to do this without a typedef, or is a typedef required? For that
matter, is a typedef ever *required*???
 
J

John Carson

Dave said:
Hello all,

Can anybody help with the problem below? I'm trying to define a
conversion function that converts objects to function pointers and am
getting a compile error on the line indicated below. The compiler
interprets this is a function returning a function, which, of course
is illegal... What is the correct syntax to accomplish this?

Thanks,
Dave


#include <iostream>

using namespace std;

double foo_func(int n) {return n + 1.5;}

class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error
here! };

int main()
{
foo_t f;

cout << f(12) << endl; // Expecting "13.5"...

return 0;
}

Do you really want to convert objects to function pointers or do you just
want an object that acts like a function? In the latter case, you can do it
with this:

class foo_t
{
public:
double operator()(int n)
{
return n + 1.5;
}
};
 
D

Dave

John Carson said:
Do you really want to convert objects to function pointers or do you just
want an object that acts like a function? In the latter case, you can do it
with this:

class foo_t
{
public:
double operator()(int n)
{
return n + 1.5;
}
};

I am indeed specifically trying to learn how to create a coversion function
to function pointer. My goal is to learn exactly this...
 
D

Dave

David White said:
here!

operator double (*())(int n) { return foo_func;}

DW

That was my next best guess too, but no go (unless it's a problem with my
compiler)!!
 
J

John Carson

Dave said:
Thx!!! Just out of curiosity, is it possible to do this without a
typedef??? I've tried a bunch of different ideas, but none work. Is
it syntactically possible to do this without a typedef, or is a
typedef required? For that matter, is a typedef ever *required*???

According to Lippman's C++ Primer (3rd ed), p. 777:


"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name.
Conversion functions in which type represents either an array or a function
type are not allowed."

This is also discussed less plainly in the C++ standard, section 12.3.2.
Apparently using function pointers without a typedef creates ambiguities in
parsing the expression.
 
D

Dave

John Carson said:
According to Lippman's C++ Primer (3rd ed), p. 777:


"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name.
Conversion functions in which type represents either an array or a function
type are not allowed."

This is also discussed less plainly in the C++ standard, section 12.3.2.
Apparently using function pointers without a typedef creates ambiguities in
parsing the expression.


Ahh, OK, cool! Can anybody cite any other cases where a typedef is
*required* to accomplish something???
 
D

David White

John Carson said:
According to Lippman's C++ Primer (3rd ed), p. 777:


"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name.
Conversion functions in which type represents either an array or a function
type are not allowed."

But you can't return an array or function from any other kind of function
either, not just a conversion operator, with or without a typedef.
This is also discussed less plainly in the C++ standard, section 12.3.2.
Apparently using function pointers without a typedef creates ambiguities in
parsing the expression.

Even if the standard doesn't allow it, I'm not convinced that the Lippman
extract has anything to do with requiring a typedef to return a function
pointer. Does he give the general form of a function as this?
type name(/*parameters*/)

which excludes the tiny fraction of functions that don't conform, such as:
double (*f())(int);

(It is just a primer, after all).

DW
 
J

John Carson

David White said:
But you can't return an array or function from any other kind of
function either, not just a conversion operator, with or without a
typedef.

The 1998 standard say this in section 8.3.5, p4:

"Functions shall not have a return type of type array or function, although
they may have a return type of type pointer or reference to such things."

The following is an example that works (without even a typedef):

#include <iostream>
using namespace std;

double f0(double n)
{
return n;
}
double f1(double n)
{
return n+1;
}
double f2(double n)
{
return n+2;
}

// this is a function taking an integer argument
// and returning a pointer to a function that
// takes a double as argument and returns a double

double (*function(int x))(double)
{
if(x==1)
return f1;
else if(x==2)
return f2;
else
return f0;
}

int main()
{
cout << function(1)(9.3)<< endl; // expect 10.3
cout << function(2)(9.3)<< endl; // expect 11.3
return 0;
}
 
D

David White

John Carson said:
The 1998 standard say this in section 8.3.5, p4:

"Functions shall not have a return type of type array or function, although
they may have a return type of type pointer or reference to such things."

The following is an example that works (without even a typedef):

#include <iostream>
using namespace std;

double f0(double n)
{
return n;
}
double f1(double n)
{
return n+1;
}
double f2(double n)
{
return n+2;
}

// this is a function taking an integer argument
// and returning a pointer to a function that
// takes a double as argument and returns a double

double (*function(int x))(double)

And in my previous post, this was a function returning a pointer to a function taking an int
parameter and returning a double:
double (*f())(int);
{
if(x==1)
return f1;
else if(x==2)
return f2;
else
return f0;
}

int main()
{
cout << function(1)(9.3)<< endl; // expect 10.3
cout << function(2)(9.3)<< endl; // expect 11.3
return 0;
}

I'm not sure what your point is. The statement you seem to have replied to remains the case,
i.e., that you can't return a function or an array from anything.

DW
 
J

John Carson

David White said:
I'm not sure what your point is. The statement you seem to have
replied to remains the case, i.e., that you can't return a function
or an array from anything.

Well...I wasn't sure what your point was. I thought (though I wasn't sure)
that you were disputing that there was a special need for typedefs where
conversion operators are concerned. I was taking it as given that we were
interested in the return of pointers to functions rather than the return of
functions.

I now think that you are simply disputing my interpretation of the Lippman
quote:

"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name.
Conversion functions in which type represents either an array or a function
type are not allowed."

You will note that Lippman makes no reference at all to pointers, yet a
conversion operator can certainly return, say, a pointer to a class type.
Accordingly, I interpreted all his statements about types as including
pointers to those types. Thus I interpret him as saying that a conversion
operator cannot return an explicit function pointer but can return a typedef
name for a function pointer.

I admit that this interpretation is arguable, but it does seem to conform to
what I observe of compiler behaviour.
 
D

David White

John Carson said:
Well...I wasn't sure what your point was. I thought (though I wasn't sure)
that you were disputing that there was a special need for typedefs where
conversion operators are concerned. I was taking it as given that we were
interested in the return of pointers to functions rather than the return of
functions.

I now think that you are simply disputing my interpretation of the Lippman
quote:

That's right.
"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name.
Conversion functions in which type represents either an array or a function
type are not allowed."

You will note that Lippman makes no reference at all to pointers, yet a
conversion operator can certainly return, say, a pointer to a class type.
Accordingly, I interpreted all his statements about types as including
pointers to those types. Thus I interpret him as saying that a conversion
operator cannot return an explicit function pointer but can return a typedef
name for a function pointer.

Yes, I'm not as sure about my interpretation now. Even a primer should get
it right, but I was unfairly implying that it might be a little sloppy.

DW
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top