0

Can I make something very similar, like this?

question ? func1(), val=5 : func2()

I'd like to put more then one instruction on the first or second parameter's place. Is it solvable?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524

4 Answers4

1

If by "instruction" (which isn't even a thing when it comes to C++'s wording), you mean "expression", then sure: parentheses and the comma operator to the rescue!

SSCCE:

#include <cstdio>

int main()
{
    int x = (1, 0) ? 2, 3 : (4, 5);
    printf("%d\n", x); // prints 5
}
  • This is a strange example, because you have no side effects in your comma operands. Your code is equivalent to `int x = 0 ? 3 : 5;`. And why `printf` in a C++ question? – Christian Hackl Jul 22 '15 at 19:05
  • @ChristianHackl what's wrong with not having any side effects? It's still an expression (so it demonstrates the answer to OP's question), isn't it? As to `printf`, [it's far superior to trying to shift a stream to the left](http://yosefk.com/c++fqa/io.html). – The Paramagnetic Croissant Jul 22 '15 at 19:14
  • Side effects are the only reason to use the comma operator in the first place. – Christian Hackl Jul 22 '15 at 19:20
  • @ChristianHackl if you follow the piece of good advice above (about avoiding utterly and unnecessarily "clever" code), then side effects are yet another reason *not to use* the comma operator. Anyway, whether or not they are a reason – my example still answers what OP was asking about. – The Paramagnetic Croissant Jul 22 '15 at 19:23
  • I don't get it. Did you deliberately post an example in which the comma operator is meaningless just to stress the fact that one should not use it for control flow? – Christian Hackl Jul 22 '15 at 19:25
  • @ChristianHackl no, I posted one without side effects in order to make the example as simple as possible. – The Paramagnetic Croissant Jul 22 '15 at 19:26
0

Yes take a look at the following example:

#include <iostream>

int main()
{
    int x,y,z;
    double d = 2.5;


    auto f = (d == 2.2) ? (x=5,y=10,z=0,2000) : (x=15,y=0,z=20,1000);

    std::cout << x << " " << y << " " << z << " " << f << std::endl;


    std::cin.get();
    return 0;
}

Not so clean so would suggest doing it more readable.

  • Note, as noted in comment on original question, that the parentheses are essential, specifically around the third argument to the ternary operator. Without them, everything after the first comma gets interpreted as following the ternary rather than part of it. See http://stackoverflow.com/questions/9189522/something-we-found-when-using-comma-in-condition-ternary-operator – Mathew Jul 22 '15 at 19:08
0

There is a discussion about the ternary operator at http://www.cplusplus.com/forum/articles/14631/ that talks about this. There are some examples in the comments of the discussion showing usage for function calls and multiple ops in the ternary operator. Though, for clarity's sake, it may be best to not do too many things at once in the ternary operator - that can get hard to read pretty quick.

Tyler
  • 197
  • 3
  • 13
0

Thanks for these fast and useful answers!

So I programming Arduino in C++, and it is the full example code:

void setup() {
  Serial.begin(115200);
  bool b = false;
  int val = 0;

  b ? func1() : (func2(), val = 2);

  Serial.println(val);
}

void loop() {}

void func1 (){
     Serial.println("func1");
}

void func2 (){
    Serial.println("func2");  
}

When I learned from this answers, how can I use brackets, and commas correctly, I got these errors:

sketch_jul22a.ino: In function 'void setup()':
sketch_jul22a:8: error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'

I used int type functions instead of void type, and the problem is solved:

int func1 (){
     Serial.println("func1");
     return 0;
}

int func2 (){
    Serial.println("func2");
    return 0;
}