ternary-operator

Ternary operator + C++11 constructor from initializer_list

泄露秘密 提交于 2021-02-16 05:39:11
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried

Ternary operator + C++11 constructor from initializer_list

这一生的挚爱 提交于 2021-02-16 05:37:02
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried

Why does ternary operator fail with a type mismatch error?

妖精的绣舞 提交于 2021-02-07 09:26:54
问题 I have the following simple code piece: List<XXXXBean> queryPeriodData() { if (CollectionUtils.isEmpty(res)) { return Collections.emptyList(); } return res; } It works. but if I change to this, there is a compile error... return CollectionUtils.isEmpty(res) ? Collections.emptyList() : res; error message is "Type mismatch: cannot convert from List< capture#1-of ? extends Object> to List< XXXXBean>" I don't know the difference between the two way. 回答1: try Collections.<XXXXBean>emptyList() in

C++, ternary operator and cout

倾然丶 夕夏残阳落幕 提交于 2021-02-05 09:32:44
问题 this code doesn't work int main(){ cout << 5 ? (5 ? 0 : 2) : 5; system("pause"); return 0; } this code works int main(){ cout << (5 ? (5 ? 0 : 2) : 5); system("pause"); return 0; } can't understand why? 回答1: cout << 5 ? (5 ? 0 : 2) : 5; is parsed as (cout << 5) ? (5 ? 0 : 2) : 5; 回答2: This is due to operator precedence rules. << has higher precedence than ? , so your first expression is parsed as: (cout << 5) ? (5 ? 0 : 2) : 5; Brackets are necessary in this case to get the parse you want. 来源

“Do nothing” in the else-part of the ternary operator?

不想你离开。 提交于 2021-02-05 08:42:17
问题 What's the standard line to add to the ternary operator in order to do nothing if the condition is not met? Example: int a = 0; a > 10 ? a = 5 : /*do nothing*/; Using a seems to do the trick, but I am wondering if there is a more generally accepted way. 回答1: That will do it: a = a > 10 ? 5 : a; or simply: if (a > 10) a = 5; 回答2: Another option: a ? void(a = 0) : void(); What's good about this one is that it works even if you can't construct an instance of decltype(a = 0) to put into the 'do

Ternary Operator in C#

不打扰是莪最后的温柔 提交于 2021-02-05 06:50:53
问题 Can anyone please explain to me what happens behind the scenes when you use ternary operator? does this line of code: string str = 1 == 1 ? "abc" : "def"; is generated as a simple if / else statement? Consider the following: class A { } class B : A { } class C : A { } Now using ternary expression as follows: A a1 = 1 == 1 ? new B() : new C(); this doesn't even compile with this error: Type of conditional expression cannot be determined because there is no implicit conversion between

Ternary Operator in C#

夙愿已清 提交于 2021-02-05 06:50:26
问题 Can anyone please explain to me what happens behind the scenes when you use ternary operator? does this line of code: string str = 1 == 1 ? "abc" : "def"; is generated as a simple if / else statement? Consider the following: class A { } class B : A { } class C : A { } Now using ternary expression as follows: A a1 = 1 == 1 ? new B() : new C(); this doesn't even compile with this error: Type of conditional expression cannot be determined because there is no implicit conversion between

Is it possible to modify or remove an anonymous type from an object in c#?

百般思念 提交于 2021-02-04 21:10:37
问题 I have a piece of code like below: var selected = “A”; bool isSelected = selected == "A" || selected == "C"; var codeLists = new { displayProperty1 = isSelected ? "property1" : null, displayProperty2 = isSelected ? "property2" : null, displayProperty3 = selected == "C" ? "property3" : null }; So, my goal is to eliminate a property if it does not satisfy a condition. In the above code, selected is "A" . So, displayProperty3 would have a value of null . But I want to eliminate displayProperty3

Performance of ternary operator vs if-else statement

允我心安 提交于 2021-01-28 03:55:08
问题 Note: It's true that this question has been answered for many other languages. However, I could not find an answer for Python, so do not mark as duplicate. Is there a difference in performance between the if-else statement and the ternary operator in Python? 回答1: I doubt there is a performance difference. They compile to equivalent sequences of bytecodes: >>> def f(): ... return a if b else c ... >>> dis.dis(f) 2 0 LOAD_GLOBAL 0 (b) 2 POP_JUMP_IF_FALSE 8 4 LOAD_GLOBAL 1 (a) 6 RETURN_VALUE >>

Avoid violation of DRY with ternary?

徘徊边缘 提交于 2021-01-28 02:06:43
问题 Here's what I have. Map data = new HashMap<>(); // assume this has been populated public int getLastestVersion() { // data.get("PATH_TO_DESIRED_POINT") would be an integer return data.get("PATH_TO_DESIRED_POINT") == null ? 0 : (int)data.get("PATH_TO_DESIRED_POINT"); } I'm trying to avoid violating DRY, but I want to be able to keep the simplicity of the ternary. Is there something I can do? 回答1: If you are using Java8, you can use getOrDefault method: return data.getOrDefault("PATH_TO_DESIRED