conditional-operator

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

醉酒当歌 提交于 2019-11-30 11:42:29
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 at a loss to explain why the first definition doesn't generate an error. Given my understanding of

Javascript one line If…else…else if statement

对着背影说爱祢 提交于 2019-11-30 10:44:38
问题 I know you can set variables with one line if/else statements by doing var variable = (condition) ? (true block) : (else block) , but I was wondering if there was a way to put an else if statement in there. Any suggestions would be appreciated, thanks everyone! 回答1: Sure, you can do nested ternary operators but they are hard to read. var variable = (condition) ? (true block) : ((condition2) ? (true block2) : (else block2)) 回答2: tl;dr Yes, you can... If a then a, else if b then if c then c(b),

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

北城余情 提交于 2019-11-30 07:54:22
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; where a simple result := result + (combo.text='')?'null':quotedStr(combo.text); would suffice. What I

Wrong type in Java conditional assignment

喜夏-厌秋 提交于 2019-11-30 06:17:27
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? Well, that is because of the JLS specs for the conditional operator : Otherwise, if the second and third

C# Conditional Operator Not a Statement?

a 夏天 提交于 2019-11-30 06:05:29
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 I reformat it as a if-then-else, it works fine. Can anyone explain the error, and if there is a way to

Strange java behaviour with conditional operator. Is it a bug?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 04:16:34
问题 Can you please run the below and explain? Object o = true ? new Integer(1) : new Double(2.0); System.out.println(o); I found that surprising as someone would expect 1 to be printed and not 1.0 回答1: It's not a surprise at all, although it might seem like one. The behaviour is specified in JLS §15.25 - Conditional Operator: Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases: If one of the operands is of type byte

One line if in VB .NET

柔情痞子 提交于 2019-11-29 22:37:08
Is it possible to do one line if statement in VB .NET? If so, how? beach Use IF(). It is a short-circuiting ternary operator. Dim Result = IF(expression,<true return>,<false return>) SEE ALSO: IIF becomes If, and a true ternary operator Is there a conditional ternary operator in VB.NET? Orcas introduces the IF operator - a new and improved IIF The Ternary Operator in VB.NET It's actually pretty simple.. If CONDITION Then ..INSERT CODE HERE.. At the risk of causing some cringing by purests and c# programmers, you can use multiple statements and else in a one-line if statement in VB. In this

Conditional statement in a one line lambda function in python?

左心房为你撑大大i 提交于 2019-11-29 20:23:51
Apologies if this has been asked before, but I couldn't see it anywhere. Essentially I've come across a scenario where i need to make use of an if statement inside a lambda function. What makes it difficult is that ideally it needs to be in a single line of code (if thats even possible?) Normally, i would write this: T = 250 if (T > 200): rate = 200*exp(-T) else: rate = 400*exp(-T) return (rate) However i need it to look like this: rate = lambda(T) : if (T>200): return(200*exp(-T)); else: return(400*exp(-T)) I realize the easier thing to do would to take the decision making outside of the

Ruby ternary operator without else

不羁的心 提交于 2019-11-29 19:30:48
Is there a ruby idiom for "If do-this," and "do-this" just as a simple command? for example, I'm currently doing object.method ? a.action : nil to leave the else clause empty, but I feel like there's probably a more idiomatic way of doing this that doesn't involve having to specify a nil at the end. (and alternatively, I feel like taking up multiple lines of code would be wasteful in this case. a.action if object.method? As a general rule: you pretty much never need the ternary operator in Ruby. The reason why you need it in C, is because in C if is a statement, so if you want to return a

Bash if [ false ] ; returns true

梦想的初衷 提交于 2019-11-29 19:03:58
Been learning bash this week and ran into a snag. #!/bin/sh if [ false ]; then echo "True" else echo "False" fi This will always output True even though the condition would seem to indicate otherwise. If I remove the brackets [] then it works, but I do not understand why. You are running the [ (aka test ) command with the argument "false", not running the command false . Since "false" is a non-empty string, the test command always succeeds. To actually run the command, drop the [ command. if false; then echo "True" else echo "False" fi A Quick Boolean Primer for Bash The if statement takes a