conditional-operator

Conditional operator with a constant (true) value?

我们两清 提交于 2019-12-03 06:38:57
问题 I was looking at some preprocessor macros used in OpenSSL, and I came across the following from crypto/stack/safestack.h : #define CHECKED_STACK_OF(type, p) \ ((_STACK*) (1 ? p : (STACK_OF(type)*)0)) #define CHECKED_SK_FREE_FUNC(type, p) \ ((void (*)(void *)) ((1 ? p : (void (*)(type *))0))) #define CHECKED_SK_FREE_FUNC2(type, p) \ ((void (*)(void *)) ((1 ? p : (void (*)(type))0))) I'm guessing its written that way to work around a compiler bug (probably something ancient that hasn't been

Does Python have the Elvis operator?

假装没事ソ 提交于 2019-12-03 06:33:07
The ternary operator in many languages works like so: x = f() ? f() : g() Where if f() is truthy then x is assigned the value of f() , otherwise it is assigned the value of g() . However, some languages have a more succinct elvis operator that is functionally equivalent: x = f() ?: g() In python, the ternary operator is expressed like so: x = f() if f() else g() But does python have the more succinct elvis operator? Maybe something like: x = f() else g() # Not actually valid python Yes Python does have the elvis operator. It is the conditional or operator: x = f() or g() f() is evaluated. If

Ternary operator behaviour inconsistency [duplicate]

♀尐吖头ヾ 提交于 2019-12-03 05:12:58
This question already has an answer here: Cannot implicitly convert type 'int' to 'short' [duplicate] 9 answers Following expression is ok short d = ("obj" == "obj" ) ? 1 : 2; But when you use it like below, syntax error occurs short d = (DateTime.Now == DateTime.Now) ? 1 : 2; Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Can anyone explain why this is so? Is there a difference between comparing string-to-string and datetime-to-datetime in a ternary operator, why? I would be grateful if you could help me. C# language specification,

Conditional Operators in Javascript

旧城冷巷雨未停 提交于 2019-12-03 05:00:59
Is it ok to use conditional operators like a statement like so? (x == y) ? alert("yo!") : alert("meh!"); Or is it more correct to use it to assign a value like so? z = (x == y) ? "yo!" : "meh!"; If it's not incorrect to use it like a statement, then is it possible to add more than one line of code for execution like so? Is it more correct to use ifthen and switch statements for multiple lines of code? (x == y) ? (alert("yo!"), document.write("woot!")) : (alert("meh!"), document.write("blah!")); Conditional operators are intentionally succinct and especially useful for assignments: var a = x ?

Convert ternary conditional operators into if statements?

房东的猫 提交于 2019-12-03 03:48:53
With minified code that looks like this, f&&!f.error?k.button.b==k.button.c.G?k.button.Q(b,e,f,c,d):k.button.b==k.button.c.o&&k.button.P(b,e,f,c,d):(console.error(f),f=f.error.message||chrome.i18n.getMessage("error_tooltip"),k.button.v(b.id,f),d({action:"error"})) Is there an automated tool that can transform that one line of conditional operators into a series of if statements? Example 1: From (i < 0 ? function1() : function2()) to if (i < 0) { function1(); } else { function2(); } Example 2: From (i < 0 ? function1() : (i === 0 ? function2() : function3())) to if (i < 0) { function1(); } else

Why does “sizeof(a ? true : false)” give an output of four bytes?

帅比萌擦擦* 提交于 2019-12-03 01:23:43
问题 I have a small piece of code about the sizeof operator with the ternary operator: #include <stdio.h> #include <stdbool.h> int main() { bool a = true; printf("%zu\n", sizeof(bool)); // Ok printf("%zu\n", sizeof(a)); // Ok printf("%zu\n", sizeof(a ? true : false)); // Why 4? return 0; } Output (GCC): 1 1 4 // Why 4? But here, printf("%zu\n", sizeof(a ? true : false)); // Why 4? the ternary operator returns boolean type and sizeof bool type is 1 byte in C. Then why does sizeof(a ? true : false)

Assignment inside Perl ternary conditional operator problems

假装没事ソ 提交于 2019-12-03 00:57:39
This snippet of Perl code in my program is giving the wrong result. $condition ? $a = 2 : $a = 3 ; print $a; No matter what the value of $condition is, the output is always 3, how come? This is explained in the Perl documentation . Because of Perl operator precedence the statement is being parsed as ($condition ? $a= 2 : $a ) = 3 ; Because the ?: operator produces an assignable result, 3 is assigned to the result of the condition. When $condition is true this means ($a=2)=3 giving $a=3 When $condition is false this means ($a)=3 giving $a=3 The correct way to write this is $a = ( $condition ? 2

Conditional operator with a constant (true) value?

空扰寡人 提交于 2019-12-02 20:19:30
I was looking at some preprocessor macros used in OpenSSL, and I came across the following from crypto/stack/safestack.h : #define CHECKED_STACK_OF(type, p) \ ((_STACK*) (1 ? p : (STACK_OF(type)*)0)) #define CHECKED_SK_FREE_FUNC(type, p) \ ((void (*)(void *)) ((1 ? p : (void (*)(type *))0))) #define CHECKED_SK_FREE_FUNC2(type, p) \ ((void (*)(void *)) ((1 ? p : (void (*)(type))0))) I'm guessing its written that way to work around a compiler bug (probably something ancient that hasn't been supported in over a decade by the vendor). What is the purpose of using the 1 above since its always true?

Why does “sizeof(a ? true : false)” give an output of four bytes?

岁酱吖の 提交于 2019-12-02 14:46:16
I have a small piece of code about the sizeof operator with the ternary operator: #include <stdio.h> #include <stdbool.h> int main() { bool a = true; printf("%zu\n", sizeof(bool)); // Ok printf("%zu\n", sizeof(a)); // Ok printf("%zu\n", sizeof(a ? true : false)); // Why 4? return 0; } Output ( GCC ): 1 1 4 // Why 4? But here, printf("%zu\n", sizeof(a ? true : false)); // Why 4? the ternary operator returns boolean type and sizeof bool type is 1 byte in C. Then why does sizeof(a ? true : false) give an output of four bytes? It's because you have #include <stdbool.h> . That header defines macros

Why do I need an explicit ToString() when using a conditional operator?

我是研究僧i 提交于 2019-12-02 08:53:48
问题 I've got the following code to compose a simple SOAP Post form data value: var postParameters = new Dictionary<string, string> { { "someNumber", "100" }, { "someString", "Hello World" } }; var resultWhenNotInConditional = postParameters .Keys .Zip(postParameters.Values, (key, value) => string.Format("{0}={1}", key, value)) .Aggregate<string, string>(null, (prev, next) => (prev != null) ? string.Format("{0}&{1}", prev, next) : next); which works as designed, viz resultWhenNotInConditional =