-4

. . . like in Algol68 or Smalltalk? The problem that this would solve is initialising a const variable for which the value is determined in an if-then-else statement.

Yes, I know, C++ is not defined like that. But what stands in the way? Would redefining the if-expression as an if statement break backward compatibility? Or would it be prohibitively difficult to disambiguate the required language documentation changes?

Example:

auto const x = if (some condition)
{   
    expression 1;
    expression 2;
    expression 3;

}
else
{
    expression 4;
}

In this case I cannot do

auto const x = (some condition) ? expression1 : expression2;
Patrick Fromberg
  • 1,313
  • 11
  • 37
  • 7
    Are you aware of the [conditional operator](https://stackoverflow.com/questions/795286/what-does-do-in-c)? – Tas Feb 12 '18 at 22:09
  • 3
    Can you show an example of what you are looking for? – NathanOliver Feb 12 '18 at 22:09
  • `int answer = x ? y : z;` meaning that if `x` is true then `answer` will be `y` else it will be `z`. – Justin Randall Feb 12 '18 at 22:14
  • 2
    Because then C++ would not be C++ it would be a different language. – Richard Critten Feb 12 '18 at 22:15
  • 2
    Playing devil's advocate (as my everyday language *does* have this feature) - what's a concrete example of why it would be *desirable*? – Oliver Charlesworth Feb 12 '18 at 22:16
  • @OliverCharlesworth , there is a difference between adding a new feature and completing a feature. My proposal is a further step in completing the const correctness feature because this is a typical case where a semantically const variable is declared non-const for practical reasons. – Patrick Fromberg Feb 12 '18 at 22:25
  • I'm not totally against it, but a change like this would require reworking `if`s as expressions instead of statements and would likely affect other types of scopes for consistency reasons. C++ is a large complex language and adding this functionality would require much consideration beyond this simple case. – kmdreko Feb 12 '18 at 22:26
  • 1
    @Patrick Fromberg: "Trigraph"??? This is not what the term "trigraph" means. – AnT stands with Russia Feb 12 '18 at 22:29
  • technically you could use the comma operator and do `(condition) ? (exp_1, exp_2, exp_3) : exp_4;` – kmdreko Feb 12 '18 at 22:29
  • I highly doubt this would be added to the language. You can already do it with a lambda. – HolyBlackCat Feb 12 '18 at 22:30
  • @PatrickFromberg Using an `if` statement (or any kind of statement) for this would be too confusing. It would require a new expression syntax, but given that you could use a function or a lambda, I imagine there won't be a huge rush to extend the language just for this. – juanchopanza Feb 12 '18 at 22:31
  • @JustinRandall, in `int answer = x ? y : z` as you suggest y and z do nos support multiple expressions. But otherwise yes, you got the idea. – Patrick Fromberg Feb 12 '18 at 22:34
  • @vu1p3n0x; in `(condition) ? (exp_1, exp_2, exp_3)` using comma, does this guarantee correct sequencing? Other than that there is some danger that the expressions returned have the comma operator overloaded. – Patrick Fromberg Feb 12 '18 at 22:37
  • @PatrickFromberg It does guarantee sequencing unless the operator is overloaded pre c++17. In c++17 it will always be in sequence. – NathanOliver Feb 12 '18 at 22:41
  • Seems like quibbling over nothing when you can quite trivially do the following: `int f(int var) { if (condition on var) { ... return a; } else { ... return b; } }` to declare a ***reusable*** function calculating your expression; followed by `auto const x = f(some value);` – Disillusioned Feb 12 '18 at 22:42
  • @PatrickFromberg it is sequenced and you can cast the expressions to `void` to guarantee that it doesn't use an overload, but that just adds to its ugliness – kmdreko Feb 12 '18 at 22:43
  • ... because it's an `if` and not a `return`... – Galik Feb 12 '18 at 22:46

1 Answers1

10

Speaking informally, C++, just like C, supports both "statement programming" and "expression programming" (see https://stackoverflow.com/a/1618867/187690 for more detail). A statement is not an expression in C++. You cannot use a statement as an expression.

In expression programming, you'd normally use ?: operator to implement branching. However, you can also embed expressions into statements and vice versa. GNU C++ compiler has been offering an extension called "statement expressions" that allowed one to embed statements into expressions. But starting from C++11 you can achieve the same in a standard way by using lambdas. E.g.

auto const x = []() 
{
  if (some condition)
  {   
    expression 1;
    expression 2;
    return expression 3;
  }
  else
  {
    return expression 4;
  }
}();

Whether your lambda will have captures and/or parameters depends on your intent.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • I marked your solution as the correct one. But the statement *You cannot use a statement as an expression* is wrong in this context because my question is exactly about redefining that and you have not proven it is not possible. Algol68 is most definitely not my favourite language as Oliver suggests (I am too young) but I wanted to show, it is a tried path. – Patrick Fromberg Feb 12 '18 at 22:47
  • @PatrickFromberg I assume the answer applies to the C++ language as it stands right now. But do you happen to have examples of programming languages where statements can be used as expressions? I can't think of one, but I don't actually know that many languages. – juanchopanza Feb 12 '18 at 23:09
  • @Patrick Fromberg: The proof that "it is not possible" (aside from the methods mentioned in the answer) is in C++ grammar. To make it exhaustive I'd have to quote the entire grammar of a C++ expression. – AnT stands with Russia Feb 12 '18 at 23:13
  • @juanchopanza, to your question: Algol68 as mentioned before but also Smalltalk. They call what is in the brackets of the `if` and `else` clause a *block*. The value of such a *block* is the value of it's last expression which is usually also the last statement. Of course an expression is always a statement but not vice versa. But not much stops you from defining `if` as an expression unless you collide with backward compatibility issues or if you cannot disambiguate resulting contradictions in the language documentation and that was my question. – Patrick Fromberg Feb 13 '18 at 22:42