ternary-operator

Casting pointers and the ternary ?: operator. Have I reinvented the wheel?

☆樱花仙子☆ 提交于 2019-12-04 02:11:49
The last line of this code fails to compile with castingAndTernary.cpp:15: error: conditional expression between distinct pointer types ‘D1*’ and ‘D2*’ lacks a cast A really smart compiler could have no difficulty because both can be safely casted to B* (the base class). I'm reluctant to use static_cast and dynamic_cast and so on - I'm worried that I'll mix up the classes someday and get undefined behaviour. That's why I created the up_cast template. This template does the bare minimum in allowed conversion. Is there a simpler way? There are other workarounds, but I can't help but think that

Can I use GCC's __builtin_expect() with ternary operator in C

為{幸葍}努か 提交于 2019-12-04 01:47:53
The GCC manual only shows examples where __builtin_expect() is placed around the entire condition of an 'if' statement. I also noticed that GCC does not complain if I use it, for example, with a ternary operator, or in any arbitrary integral expression for that matter, even one that is not used in a branching context. So, I wonder what the underlying constraints of its usage actually are. Will it retain its effect when used in a ternary operation like this: int foo(int i) { return __builtin_expect(i == 7, 1) ? 100 : 200; } And what about this case: int foo(int i) { return __builtin_expect(i, 7

Why does assigning a null value from a ternary statement to a Boolean variable throw a NPE? [duplicate]

浪尽此生 提交于 2019-12-04 01:46:08
This question already has an answer here : Closed 5 years ago . Strange Java behaviour. Ternary operator (1 answer) I have a part of code like this: public static void main(String[] args) throws Exception { String trueValue = Boolean.TRUE.toString(); String fieldValue = null; Boolean defaultValue = null; Boolean value = (fieldValue != null ? trueValue.equalsIgnoreCase(fieldValue) : defaultValue); System.out.println(value); } When defaultValue is not equal to null the code works fine, but when defaultValue is null the JVM throws a NullPointerException . This code was compiled using jdk 1.6.45.

Weird behaviour when using Java ternary operator

我们两清 提交于 2019-12-04 00:27:48
When I write my java code like this: Map<String, Long> map = new HashMap<>() Long number =null; if(map == null) number = (long) 0; else number = map.get("non-existent key"); the app runs as expected but when I do this: Map<String, Long> map = new HashMap<>(); Long number= (map == null) ? (long)0 : map.get("non-existent key"); I get a NullPointerException on the second line. The debug pointer jumps from the second line to this method in the java.lang.Thread class: /** * Dispatch an uncaught exception to the handler. This method is * intended to be called only by the JVM. */ private void

Using ternary operator to initialize a reference variable?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 23:25:10
Putting all the maintainability and reading issues aside, can these lines of code generate undefined behavior? float a = 0, b = 0; float& x = some_condition()? a : b; x = 5; cout << a << ", " << b; No, it's just fine. It would not create undefined behavior in this code. You will just change value of a or b to 5, according to condition. This is absolutely fine, as long as both sides of the conditional are expressions that can be used to initialize a reference (e.g. variables, pointer dereferences, etc) float& x = some_condition()? a : *(&b); // This is OK - it is the same as your code float& x

Incompatible operand types when using ternary conditional operator

青春壹個敷衍的年華 提交于 2019-12-03 22:49:46
This code: bool contains = std::find(indexes.begin(), indexes.end(), i) != indexes.end(); CardAbility* cardAbility = contains ? new CardAbilityBurn(i) : new CardAbilityEmpty; gives me the following error: Incompatible operand types CardAbilityBurn and CardAbilityEmpty However if I write the code like this: if (contains) { cardAbility = new CardAbilityBurn(i); } else { cardAbility = new CardAbilityEmpty; } then the compiler doesn't mind. Why so? I want to use ternary conditional operator because it is just one line. What's wrong there? I need to note (I think you might need this information)

JS Ternary functions with multiple conditions?

你离开我真会死。 提交于 2019-12-03 17:45:13
问题 I have been using a ternary operator in JavaScript to modify the value of an object based on user input. I have the following code, which runs as it should: var inputOneAns = inputOne == "Yes" ? "517" : "518"; As you can see, I am assigning a numeric string value to inputOneAns whether a user has inputed "Yes" or "No". However, there may be a case that a user has not selected a value (as it is not required). If this input was left blank, I would like to assign an empty string "" to

Ternary operator in C vs C++ [duplicate]

与世无争的帅哥 提交于 2019-12-03 12:38:44
This question already has answers here : Errors using ternary operator in c (5 answers) There are a lot of differences between C and C++ and came to stuck on one of them The same code gives an error in C while just executes fine in C++ Please explain the reason int main(void) { int a=10,b; a>=5?b=100:b=200; } The above code gives an error in C stating lvalue required while the same code compiles fine in C++ Have a look at the operator precedence. Without an explicit () your code behaves like ( a >= 5 ? b = 100 : b ) = 200; The result of a ?: expression is not a modifiable lvalue [#] and hence

Expressions in JavaScript Ternary Operator and JSLint

风格不统一 提交于 2019-12-03 10:52:07
问题 I recently received a comment on one of my blog posts about JSLint asking why JSLint threw an error with the following: s === "test" ? MyFunc() : MyFunc2(); The error generated was: "Expected an assignment or function call and instead saw an expression." Clearly JSLint is expecting an assignment here, somthing more like: var y = (s === "test") ? MyFunc() : MyFunc2(); But, I don't really see the problem with the first example. Is it really the case that ternary operators should only be used

How to make Resharper format line-wrapped ternary operators in the following way

戏子无情 提交于 2019-12-03 07:24:08
I really want Resharper to format my line-wrapped ternaries in this way return navigator.IsTerminating ? navigator.Context : navigator.Context.GetSimulatableRelative(new Navigator(navigator)); Can anyone help with this? I don't think there is an out of the box way of doing this. The only way i know to influence how R# formats code is through these settings: Resharper --> Options... --> Langauges --> C# --> Formatting Style This doesn't allow you to write your own custom formatting rules though. If you feel like a challenge then you could write a plugin. There is some docs and an SDK: Developer