conditional-operator

Why does this function return an lvalue reference given rvalue arguments?

我的梦境 提交于 2019-11-29 17:26:25
问题 The following definition of a min function template <typename T, typename U> constexpr auto min(T&& t, U&& u) -> decltype(t < u ? t : u) { return t < u ? t : u; } has a problem: it seems that it's perfectly legal to write min(10, 20) = 0; This has been tested with Clang 3.5 and g++ 4.9. The solution is straightforward, just use std::forward to restore the "rvalue-ness" of the arguments, i.e. modify the body and the decltype to say t < u ? std::forward<T>(t) : std::forward<U>(u) However, I'm

C# conditional operator error Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

我怕爱的太早我们不能终老 提交于 2019-11-29 14:05:08
Hi I was writing a basic program to find if the input number is prime or not. I have a checkPrime(num) function that checks for prime number and returns true if num is prime else returns false . Now in my main() function I used conditional operator to shorten the code but I am getting an error which I am not sure about. Below is my code : static void Main(String[] args) { int n = Int32.Parse(Console.ReadLine()); while (n-- > 0) { int num = Int32.Parse(Console.ReadLine()); (checkPrime(num) == true) ? Console.WriteLine("Prime") : Console.WriteLine("Not Prime"); } } When I compile, I get the

Is there, or is there ever going to be, a conditional operator in Delphi?

▼魔方 西西 提交于 2019-11-29 10:41:21
问题 I kept my hands off Delphi for too long, I guess; busied myself with Java and PHP a lot over the last couple of years. Now, when I got back to doing a little Delphi job, I realised I really miss the conditional operator which is supported by both Java and PHP. On how many places would you find lines like these in your Delphi programs? var s : string; begin ...<here the string result is manipulated>... if combo.Text='' then s := 'null' else s := QuotedStr(combo.Text); result := result + s; end

Wrong type in Java conditional assignment

僤鯓⒐⒋嵵緔 提交于 2019-11-29 06:06:01
问题 In the following code I have two identical conditional assignment operations, one returns an object of type Double, and the second returns the String "Integer". double d = 24.0; Number o = (d % 1 == 0) ? new Double(d).intValue() : new Double(d).doubleValue(); String result = (d % 1 == 0) ? "Integer" : "Double"; System.out.println(o.getClass()); // prints "class java.lang.Double" System.out.println(result); // Integer Why are the exact same expressions returning two different things? 回答1: Well

IE Conditional operator: OR … if is greater than ie9 or not IE

痴心易碎 提交于 2019-11-29 05:55:53
I want to only include history and ajaxify if the browser is ie9 or greater, OR is not ie: <!--[if gte IE 9]> <script type="text/javascript" src="assets/js/plugins/history.js"></script> <script type="text/javascript" src="assets/js/plugins/ajaxify.js"></script> <![endif]--> How can I use the OR operator to say this: <!--[if gte IE 9 | !IE ]> ?? Thanks! This worked: <!--[if gte IE 9 | !IE ]><!--> <script type="text/javascript" src="assets/js/plugins/history.js"></script> <script type="text/javascript" src="assets/js/plugins/ajaxify.js"></script> <![endif]--> Yeah, the or operator is: | . You

C# Conditional Operator Not a Statement?

纵然是瞬间 提交于 2019-11-29 05:49:20
问题 I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs = new HashSet<long>(); groupUIDs.Add(uid)? unique++ : dupes++; At compile time, it generates the error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement HashSet.Add is documented to return a bool, so the ternary (?) operator should work, and this looks like a completely legitimate way to track the number of unique and duplicate items I add to a hash-set. When

Is it possible to use EL conditional operator in action attribute?

醉酒当歌 提交于 2019-11-29 01:09:19
The conditional operator works in many attributes like "rendered" "value" and others. But it does not work in action? Or am I doing it wrong? <h:commandLink action="#{true ? bean.methodTrue() : bean.methodFalse()}"/> Error: javax.el.ELException: Not a Valid Method Expression (I realized it using primefaces ajax action attribute) This is not supported. The action attribute is supposed to be a MethodExpression , but the conditional operator makes it a ValueExpression syntax. I don't think this will ever be supported for MethodExpression s in EL. You have basically 2 options: Create a single

Are multiple conditional operators in this situation a good idea?

≡放荡痞女 提交于 2019-11-28 23:18:41
I just saw this block of code on the Wikipedia article on conditional operators: Vehicle new_vehicle = arg == 'B' ? bus : arg == 'A' ? airplane : arg == 'T' ? train : arg == 'C' ? car : arg == 'H' ? horse : feet; I've changed the code a little, but the idea is the same. Would you find this use of the conditional operator acceptable? It's much more concise than the if - else construct, and using a switch would definitely open up a whole new set of opportunities for bugs (fall-throughs anyone?). Also, if - else s and switch can't be used as R-values, so you'd have to create the variable first,

How to write a ternary operator (aka if) expression without repeating yourself

爷,独闯天下 提交于 2019-11-28 19:24:19
问题 For example, something like this: var value = someArray.indexOf(3) !== -1 ? someArray.indexOf(3) : 0 Is there a better way to write that? Again, I am not seeking an answer to the exact question above, just an example of when you might have repeated operands in ternary operator expressions... 回答1: Personally I find the best way to do this is still the good old if statement: var value = someArray.indexOf(3); if (value === -1) { value = 0; } 回答2: Code should be readable, so being succinct should

twig: IF with multiple conditions

倾然丶 夕夏残阳落幕 提交于 2019-11-28 15:50:41
It seem I have problem with a twig if statement. {%if fields | length > 0 || trans_fields | length > 0 -%} The error is: Unexpected token "punctuation" of value "|" ("name" expected) in I can't understand why this doesn't work, it's like if twig was lost with all the pipes. I've tried this : {% set count1 = fields | length %} {% set count2 = trans_fields | length %} {%if count1 > 0 || count2 > 0 -%} but the if also fail. Then tried this: {% set count1 = fields | length > 0 %} {% set count2 = trans_fields | length > 0 %} {%if count1 || count2 -%} And it still doesn't work, same error every time