4

The original code is:

if (expression1) statement1;
    else statement2;

is it possible to transform it into this?

expression1 ? statement1 : statement2

or it depends on the compiler? it seems that this differs within c++ standards?

Sometimes the case is if (expression1) statement1; and how can i transform that?

btw, it can be done in c.

Making the source code unreadable is exactly what i am trying to do
This is just one of the steps

error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
This is what i got with g++ (TDM-2 mingw32) 4.4.1 when compile

#include <stdio.h>

void _(int __, int ___, int ____, int _____)
{
    ((___ / __) <= _____) ? _(__,___+_____,____,_____) : !(___ % __) ?     _(__,___+_____,___ % __, _____) :
    ((___ % __)==(___ / __) && !____) ? (printf("%d\n",(___ / __)),
    _(__,___+_____,____,_____)) : ((___ % __) > _____ && (___ % __) < (___ / __)) ?
    _(__,___+_____,____,_____ + !((___ / __) % (___ % __))) : (___ < __ * __) ?
    _(__,___+_____,____,_____) : 0;
}

int main() {
    _(100,0,0,1);
    return 0;
}

and if i replace the last 0 with throw 0, it will compile successfully.

iloahz
  • 4,491
  • 8
  • 23
  • 31
  • Why would you want to do that? It's (often) less readable and it won't be any faster or something like that. – KillianDS Jul 04 '11 at 15:35
  • Although you _can_ write code like that, it is not advisable, as it is less comprehensible. GCC even has an extension that lets you write even less readable code such as `x ?: y` which means as much as `if(x) x else y`. But again, code should first be readable and comprehensible. – Damon Jul 04 '11 at 15:37
  • @KillianDS @Damon that's just what i am trying to do. ^^ – iloahz Jul 04 '11 at 15:50
  • @Topro apart from _, all your other identifiers are reserved to the implementation for any use [global.names] – Come Raczy Sep 12 '14 at 20:41

7 Answers7

10

expression1 ? statement1 : statement2 This is actually incorrect. The correct is this:

expression1 ? expression2 : expression3

Not any statement can be equivalently transformed into a single expression, so in general it is not always possible. For example:

if(expr)
{
   for(int i = 0; i < 2; ++i) {std::cout << i; }
}
else
{ 
   //something else
}

You can't transform this into ?: expression because for is a statement, not an expression.

Btw. It can't be done in standard C. What you are referring to is probably the statement expression which is a GCC extension.

H.S.
  • 11,654
  • 2
  • 15
  • 32
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
3

You can transform the single if

if (expression1) expression2;

with a trick of the (void(0)) this ways

expression1 ? expression2 : (void(0)) ;

but I am not suggest to do it/use it !

the ? : can lead to spaghetti code and try to avoid it. Its better to have a clear and easy to read and understand code.

Also the code is the same on both ways and there is not special reason to make a hard to read code.

Aristos
  • 66,005
  • 16
  • 114
  • 150
1

Making the source code unreadable is exactly what i am trying to do

Why? The only people you are going to affect are the maintainers of your code. If you are trying to keep hackers from understanding your code, don't bother. They know every trick in the book, and several that aren't in the book.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
0

The code:

expression1 ? statement1 : statement2

works the same in C++ as it does in (GCC) C.

The code:

if (expression1) statement1;

Is already as simple as things go. Why would you even want to transform this?

J T
  • 4,946
  • 5
  • 28
  • 38
0

In addition to Armen answer, it may be mentioned that C grammar defines statement as one of the following: labeled_statement, compound_statement, expression_statement, selection_statement, iteration_statement and jump_statement. Of all of them, only expression statement is, actually, an expression.

Mihails Strasuns
  • 3,783
  • 1
  • 18
  • 21
  • @Михаил: Actually, an expression statement is not an expression either. An expression statement contains a ';' in the end, unlike an expression :) – Armen Tsirunyan Jul 04 '11 at 15:55
  • Thanks for the point. I was intending more to list options of statements not formed from expression, though. My bad, should have been more formal in wording. – Mihails Strasuns Jul 04 '11 at 16:00
0

Not related to the ?: issue, but I don't think it is legal to define your own identifiers with double underscores, or identifiers beginning with underscores in the global namespace - see this question.

Community
  • 1
  • 1
James
  • 3,191
  • 1
  • 23
  • 39
0

?: is a fun operator.....really it is just a style choice however, there is no loss in readability to using it.

Any time you have a simple If/Else block setting a binary variable it is a terrific choice.

If (exp1)
{
var = exp2;
}
else
{
var = exp3;
}

This can easily be transformed to a ? ternary operator.

var = Exp1 ? Exp2 : Exp3;

Exp1 the condition from the original If statement is evaluated. If Exp1 is (true), Exp2 is evaluated and becomes the value of the statement, if Exp1 is (false) then Exp3 is evaluated and becomes the value of the statement.

If you use the ? ternary with inline functions in Exp2 and Exp3 the line length can get unreadably long while allowing you to do just about anything, which satisfies the opening question.

Mostly I keep it short and use this operator in guard statements to make overrun safe code. It's visually distinctive which I personally feel adds greatly to the readability.