0

Wasn't sure how to title this question...

I was curious whether the title example is possible somehow, and are there any good tricks to using it?

I have been using something similar quite a lot:

newBool = (someTest) ? true:false;
newInt  = (x + (y / z));
[...]

However, I was curious if it was possible to add far more conditions, almost like a function?

For some simple(*) examples:

someThing = { 
    int _x = aFunction(_x);
    if(_x) { 
        something = somethingElse;
        for(...){ ... }; // etc
    } else {
        /* otherStuff */
    } 
};

Or

someThing = {
    for(_x=0;_x<y;_x++){
        z++;
    }
    someThing = (z>y) ? a:b;
};

I understand it's worth just separating it off into functions most of the time, although I'm trying to get away from a function/library-fest as much as possible... It's getting annoying crawling through everything when I could just have the useful stuff right there, where I need it, instead of 1000 lines away, or in another library. It also allows the text editor to fold the code out of sight, starting from one variable, instead of on each clause.

With the few tests I have tried, I've not been successful in achieving what I'm experimenting with, so, is this kind of thing possible in any way? and if so, what are the rules/possibilities/different ways you could implement this?

* Emphasis on simple! And a semi-pointless/completely useless "function" alert too! I'm just curious about the "proof of concept" in the examples...

closed as unclear what you're asking by user31481, gre_gor, per1234, MatsK, jose can u c Dec 19 '17 at 15:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Something = {code} is just syntatic sugar for code; something=someotherthing – user31481 Dec 16 '17 at 16:32
2

You can do it with a lambda function, though the syntax is somewhat cryptic:

int someThing = [](int y, int z, int a, int b){
    for (int _x = 0; _x < y; _x++) {
        z++;
    }
    return (z > y) ? a : b;
}(100, 0, 10, 20);

In that you are defining a lamda function [] with four parameters, y, z, a and b. Then calling that function with four numbers (100, 0, 10, 20) and assigning the return value to someThing.

It's the same as writing a normal function but there's a lot more brackets around.

  • That gave me inner smiles :D That's a really nice example! Thank you! I assume (100, 0, 10, 20) could be (intOne, intTwo, boolOne, boolTwo), for example, too? – Timeless Dec 16 '17 at 17:00
  • Yep. Just like calling any function. – Majenko Dec 16 '17 at 17:27
  • I just don't understand what is the gain doing this. It's overkill using lambdas for something you can in-line for the same result with less code. – user31481 Dec 16 '17 at 21:03
  • @LookAlterno No benefit at all. The compiler will probably optimize it away anyway. But.. why climb a mountain...? Cus it's there. – Majenko Dec 16 '17 at 21:04
  • Then, some time latter, some poor soul have to mantain that code ... – user31481 Dec 16 '17 at 21:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.