473,398 Members | 2,113 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,398 software developers and data experts.

Function call selection using ternary operator

Hi,

I am wondering if there is a way to use the ternary operator to select
a function, between two, even if at least one of them is overloaded.
Ex:

void f1(int) {}
void f1(double) {}
void f2(int) {}
//void f2(double) {}

int main()
{
bool bTest = false;
(bTest ? f1 : f2)(int(1)); // this statement does not compile
return 0;
}

TIA.
Marco
Jul 22 '05 #1
4 6019

"marco_segurini" <ma***********@virgilio.it> wrote in message
news:a3**************************@posting.google.c om...
Hi,

I am wondering if there is a way to use the ternary operator to select
a function, between two, even if at least one of them is overloaded.
Ex:

void f1(int) {}
void f1(double) {}
void f2(int) {}
//void f2(double) {}

int main()
{
bool bTest = false;
(bTest ? f1 : f2)(int(1)); // this statement does not compile


bTest ? f1(int(1)) : f2(int(1));

Regards,
Sumit.
--
Sumit Rajan <sumitrajan AT alexandria DOT cc>
Jul 22 '05 #2
"Sumit Rajan" <su********@yahoo.com> wrote in message news:<2r*************@uni-berlin.de>...
"marco_segurini" <ma***********@virgilio.it> wrote in message
news:a3**************************@posting.google.c om...
Hi,

I am wondering if there is a way to use the ternary operator to select
a function, between two, even if at least one of them is overloaded.
Ex:

void f1(int) {}
void f1(double) {}
void f2(int) {}
//void f2(double) {}

int main()
{
bool bTest = false;
(bTest ? f1 : f2)(int(1)); // this statement does not compile


bTest ? f1(int(1)) : f2(int(1));


Thanks for your reply Sumit.

This solution is the same as use:

if (bTest)
f1(1);
else
f2(1);

My goal is to avoid to repeat the actual parameters list (1).

Bye.
Marco.
Jul 22 '05 #3
In message <a3**************************@posting.google.com >,
marco_segurini <ma***********@virgilio.it> writes
"Sumit Rajan" <su********@yahoo.com> wrote in message
news:<2r*************@uni-berlin.de>...
"marco_segurini" <ma***********@virgilio.it> wrote in message
news:a3**************************@posting.google.c om...
> Hi,
>
> I am wondering if there is a way to use the ternary operator to select
> a function, between two, even if at least one of them is overloaded.
> Ex:
>
> void f1(int) {}
> void f1(double) {}
> void f2(int) {}
> //void f2(double) {}
>
> int main()
> {
> bool bTest = false;
> (bTest ? f1 : f2)(int(1)); // this statement does not compile


bTest ? f1(int(1)) : f2(int(1));


Thanks for your reply Sumit.

This solution is the same as use:

if (bTest)
f1(1);
else
f2(1);

My goal is to avoid to repeat the actual parameters list (1).


You need a typedef to disambiguate the pointer to the overloaded
function:.

typedef void (*FInt)(int);
typedef void (*FDouble)(double); // etc

(bTest ? FInt(f1) : FInt(f2)) (1);
--
Richard Herring
Jul 22 '05 #4
ma***********@virgilio.it (marco_segurini) wrote in message news:<a3**************************@posting.google. com>...
Hi,

I am wondering if there is a way to use the ternary operator to select
a function, between two, even if at least one of them is overloaded.
Ex:

void f1(int) {}
void f1(double) {}
void f2(int) {}
//void f2(double) {}

int main()
{
bool bTest = false;
(bTest ? f1 : f2)(int(1)); // this statement does not compile
return 0;
}

TIA.
Marco

use function pointers:

void f1(int x)
{
}

void f2(int x)
{
}

int main()
{
bool bTest = true;

void (*f)(int x);

f = bTest ? f1 : f2;
f(3);

return 0;
}

Dan
Jul 22 '05 #5

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

Similar topics

2
by: Sigmund | last post by:
Hi, I would like to put an IF statement within the mail() function. I've tried this and searched around but haven't found anything written about this. Is it possible? I've been doing somethings...
15
by: Adal Chiriliuc | last post by:
I think a function similar to the one below should be added to the builtin module: def boolselect(condition, trueresult, falseresult): if condition: return trueresult else: return falseresult...
6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
1
by: Mark Livingstone | last post by:
Hello! I am a Uni student and for a project in Information Systems Security due in just under two weeks, I have tried to make a Python version of the Biham / Anderson Tiger Hash function. I have...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...
0
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,...
0
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...

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.