C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fun with ptr_fun

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Glen Low
Guest





PostPosted: Mon Sep 15, 2003 9:55 am    Post subject: Fun with ptr_fun Reply with quote



How do you select one of a number of overloaded functions (short of
using typeof?) for std::ptr_fun?

E.g.

int func (int i);
float func (float f);

int main ()
{
std::ptr_fun (func); // ambigious, which func is meant?
}

It's not exactly a std::ptr_fun_t I'm after, since I want really want
to do is, have a (function) template that is partially specialized on
various forms of function, but if I can't select a particular one of a
number of overloaded functions, how could it work?

Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ivan Vecerina
Guest





PostPosted: Mon Sep 15, 2003 3:45 pm    Post subject: Re: Fun with ptr_fun Reply with quote



Hi Glen,
"Glen Low" <glenlow (AT) pixelglow (DOT) com> wrote

Quote:
How do you select one of a number of overloaded functions (short of
using typeof?) for std::ptr_fun?

E.g.

int func (int i);
float func (float f);

int main ()
{
std::ptr_fun (func); // ambigious, which func is meant?
}

It's not exactly a std::ptr_fun_t I'm after, since I want really want
to do is, have a (function) template that is partially specialized on
various forms of function, but if I can't select a particular one of a
number of overloaded functions, how could it work?

Immediately casting the function name is how you can specify
a specific overload:
std::ptr_fun( ( int(*)(int) ) func );

The cast is used at compile time to resolve the name 'func'...

hth
--
http://ivan.vecerina.com



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Mon Sep 15, 2003 3:46 pm    Post subject: Re: Fun with ptr_fun Reply with quote



Glen Low wrote:
Quote:
How do you select one of a number of overloaded functions (short of
using typeof?) for std::ptr_fun?

int func (int i);
float func (float f);

std::ptr_fun (func); // ambigious, which func is meant?

// disclaimer: uncompiled code ahead
typedef float fffunc (float f);
std::ptr_fun((fffunc*)&func);

Not sure how that is related to ptr_fun, but taking the address of a
particular function of a set of overloads is done by a simple cast. Yes,
its really ugly but it seems to be the only way there is. :(

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gabriel Dos Reis
Guest





PostPosted: Mon Sep 15, 2003 3:47 pm    Post subject: Re: Fun with ptr_fun Reply with quote

[email]glenlow (AT) pixelglow (DOT) com[/email] (Glen Low) writes:

Quote:
How do you select one of a number of overloaded functions (short of
using typeof?) for std::ptr_fun?

by explicit cast-notation, either with old-style cast or with
static_cast<> -- yes, this is a hack, but a standard hack.

Quote:

E.g.

int func (int i);
float func (float f);

int main ()
{
std::ptr_fun (func); // ambigious, which func is meant?

std::ptr_fun ((int (*)(int)) &func);
std::ptr_fun(static_cast<float (*)(float)>(&fun));

Quote:
}

It's not exactly a std::ptr_fun_t I'm after, since I want really want
to do is, have a (function) template that is partially specialized on
various forms of function, but if I can't select a particular one of a
number of overloaded functions, how could it work?

hardly. Unless you want overload sets to be first class values.

--
Gabriel Dos Reis <gdr (AT) integrable-solutions (DOT) net>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Glen Low
Guest





PostPosted: Mon Sep 15, 2003 9:53 pm    Post subject: Re: Fun with ptr_fun Reply with quote

After whacking around the bit of code, I think I've answered my own
question:

Quote:
int func (int i);
float func (float f);

int main ()
{
std::ptr_fun (func); // ambigious, which func is meant?
}

To select the first:

std::ptr_fun (static_cast <int (*) (int)> (func));

To select the second:

std::ptr_fun (static_cast <float (*) (float)> (func));

My requirement in its bare essentials is to devise a compile-time
expression that uniquely selects one of the funcs. It appears that:

static_cast <int (*) (int)> (func)

and

static_cast <float (*) (float)> (func)

are so.

Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Mon Sep 15, 2003 10:02 pm    Post subject: Re: Fun with ptr_fun Reply with quote

In article <9215d7ac.0309142005.4251f874 (AT) posting (DOT) google.com>,
Glen Low wrote:
Quote:
How do you select one of a number of overloaded functions
(short of using typeof?) for std::ptr_fun?

E.g.

int func (int i);
float func (float f);

int main ()
{
std::ptr_fun (func); // ambigious, which func is meant?
}
snip


Use a static cast:

typedef float (*func_type)(float);
std::ptr_fun(func_type(func));

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Tue Sep 16, 2003 7:03 am    Post subject: Re: Fun with ptr_fun Reply with quote

"Glen Low" <glenlow (AT) pixelglow (DOT) com> wrote in message

Quote:
int func (int i);
float func (float f);

In addition to static_cast you can also declare a variable of the right type

int (*const ptr)(int) = &func;

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Shay
Guest





PostPosted: Wed Sep 17, 2003 9:40 am    Post subject: Re: Fun with ptr_fun Reply with quote

In article <9215d7ac.0309150642.66fc6ee5 (AT) posting (DOT) google.com>,
[email]glenlow (AT) pixelglow (DOT) com[/email] (Glen Low) wrote:
Quote:
After whacking around the bit of code, I think I've answered my own
question:

int func (int i);
float func (float f);

int main ()
{
std::ptr_fun (func); // ambigious, which func is meant?
}

To select the first:

std::ptr_fun (static_cast <int (*) (int)> (func));

To select the second:

std::ptr_fun (static_cast <float (*) (float)> (func));
[snip]


I noticed that about half the replies to your original post contained
suggestions to use C-style casts, leading to a hidden reinterpret_cast<>
(and undefined behavior if invoked) if the types differed:

int func( int );

(float (*)( int )) func // oops, but no compile error

static_cast<float (*)( int )> (func) // compile error

--
Shay

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Wil Evers
Guest





PostPosted: Wed Sep 17, 2003 8:28 pm    Post subject: Re: Fun with ptr_fun Reply with quote

In article <postmaster-1609031240220001 (AT) aus-as2-003 (DOT) io.com>, Shay wrote:

Quote:
I noticed that about half the replies to your original post contained
suggestions to use C-style casts, leading to a hidden reinterpret_cast
(and undefined behavior if invoked) if the types differed:

int func( int );

(float (*)( int )) func // oops, but no compile error

static_cast<float (*)( int )> (func) // compile error

Sure, a static_cast is better than a C-style cast. But then, no cast is
better than a static_cast:

int func(int);
float func(float);

int main()
{
int (*pf)(int) = func;
std::ptr_fun(pf);
return 0;
}

- Wil

--
Wil Evers, DOOSYS R&D BV, Utrecht, Holland
[Wil underscore Evers at doosys dot com]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.