anything whcih can be done with ternary but not with if else

G

gupta.keshav

HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?
Thanks
Keshav
 
R

Rolf Magnus

HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?

class Foo
{
public:
Foo(bool b)
: x_(b ? 42 : 7)
{
}
private:
const int x_;
};
 
M

mlimber

Rolf said:
class Foo
{
public:
Foo(bool b)
: x_(b ? 42 : 7)
{
}
private:
const int x_;
};

And conditionally assigning constants in general. Compare:

void Foo( const bool b )
{
const int i = b ? 1 : 2;
// ...
}

void Bar( const bool b )
{
// i cannot be const
int i = 0;
if( b )
{
i = 1;
}
else
{
i = 2;
}
// ...
}

Cheers! --M
 
J

John Carson

HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?
Thanks
Keshav

bool b;
int x, y;

// stuff setting b, x and y

int & ref = b ? x, y;

Note that you can do

if(b)
int & ref = x;
else
int & ref = y;

but the scope of the reference is limited to the body of the if-else
conditions. You can't get around this by declaring the reference before the
if statement because a reference must be initialised at its point of
declaration.
 
S

shailendra

what if I'm doing this?

int const * foo( const bool b )
{
static int i = 0;

if( b )
{
i = 1;
}
else
{
i = 2;
}
return &i;
}
 
F

Fraser Ross

int const * foo( const bool b )
{
static int i;
return &(i=b ? 1 : 2);
}


This is the equivalent using the ternary operator making the function as
short as possible. This doesn't give an example that the OP is looking
for.

Fraser.
 
M

mlimber

shailendra said:
what if I'm doing this?

int const * foo( const bool b )
{
static int i = 0;

if( b )
{
i = 1;
}
else
{
i = 2;
}
return &i;
}

It should work fine, and you could use the ternary operator, though not
on the initialization of the static:

int const* foo( const bool b )
{
static int i = 0; // Only executed once
i = b ? 1 : 2; // Executed each time foo is called
return &i;
}

Cheers! --M
 
S

shailendra

My reply was to the examples given by "mlimber" that, there is a way to
substitute his/her idea --

So given example is wrong, coz this isn't the only situatuation where
ternery can be applied. So we better find some good situation only for
ternery.

Frankly speaking, I dont think, there is any!!
 
J

Jay Nabonne

HI,
Is there any situation which can be handled by ternary operator but
not with if else blocks?

I'm not sure there is such a case *if* you're allowed to put the if/else
in a separately called function.

e.g.

class Foo
{
public:
Foo(bool b)
: x_(getit(b))
{
}

int getit(bool b)
{
if (b)
return 42;
else
return 7;
}
private:
const int x_;
};

- Jay
 
M

Mike Smith

shailendra said:
My reply was to the examples given by "mlimber" that, there is a way to
substitute his/her idea --

So given example is wrong, coz this isn't the only situatuation where
ternery can be applied. So we better find some good situation only for
ternery.

Frankly speaking, I dont think, there is any!!

One has already been given.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Is there any situation which can be handled by ternary operator but
not with if else blocks?

if-else requires assignability. Anything that doesn't support assignment
cannot be used with if-else. In addition to the already provided constructor
initialization list, three more that I can think of:


1) A class that doesn't define operator=:

Class UnAssignable{ /* ... */ };

/* copy construction */
UnAssignable object = condition ? first : second;


2) Initializing a reference:

Class & reference = condition ? first : second;


3) Initializing a constant:

SomeType const object = condition ? first : second;

Ali
 
P

persenaama

You aren't replacing the ternary operator with if-else block but with a
function call with if-else block inside it. There's a subtle
difference, I think. A good suggestion, just happened to think, "wait
just a minute...!"
 
O

Old Wolf

mlimber said:
And conditionally assigning constants in general. Compare:

void Foo( const bool b )
{
const int i = b ? 1 : 2;
// ...
}

In this case it can be done with if...else:
void Foo( const bool b )
{
int i_;
if (b)
i_ = 1;
else
i_ = 2;
const int i = i_;
}
 
O

Old Wolf

Rolf said:
class Foo
{
public:
Foo(bool b)
: x_(b ? 42 : 7)
{
}
private:
const int x_;
};

Note that this doesn't imply that ?: adds any new functionality
to the language; (b ? 42 : 7) is equivalent to (b * 35 + 7).

However I can't think of a replacment for (b ? foo() : bar()) offhand.
 
G

Greg Comeau

What can also be done with ternary is it can be put
into a comma operator list, perhaps as part of a macro
 
R

Rolf Magnus

Greg said:
What can also be done with ternary is it can be put
into a comma operator list, perhaps as part of a macro

And it can be used in template arguments.
 
J

John Carson

wht can be done is

bool b;
int x,y,z;
if (b) z= x; else z = y;
int &i = z;

That is not equivalent to

int &i = b ? x, y;

Suppose that the code is followed by:

i = 6;

With my code, either x or y will become 6. With your code, x and y are not
affected; the change is to z.
 
B

ben

Old said:
Note that this doesn't imply that ?: adds any new functionality
to the language; (b ? 42 : 7) is equivalent to (b * 35 + 7).

However I can't think of a replacment for (b ? foo() : bar()) offhand.

int f(bool b)
{
if (b) return foo();
else return bar();
}

class Foo
{
public;
Foo(bool b)
: x_(f(b))

.....

ben
 
J

Jay Nabonne

You aren't replacing the ternary operator with if-else block but with a
function call with if-else block inside it. There's a subtle
difference, I think. A good suggestion, just happened to think, "wait
just a minute...!"

You're quite right. I mostly mentioned it because, believe it or not, I
have seen coding guidelines that forbid the use of the ternary operator!
So knowing how to live without it can be useful (though I wouldn't want to).

- Jay
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top