ternary-operator

May a compiler elide the evaluation of the “not-taken” branch in a constexpr function?

谁说我不能喝 提交于 2020-01-05 10:19:23
问题 While attempting to answer a question by Mehrdad, I concocted the little function below (in action at liveworkspace): template <typename T, unsigned low, unsigned high> static constexpr auto highest_index_in() -> typename std::enable_if<high >= low, unsigned>::type { return low == high ? low : high == low + 1 ? (exists<T, high>() ? high : low) : exists<T, (high + low)/2>() ? highest_index_in<T, (high+low)/2, high>() : highest_index_in<T, low, (high+low)/2>(); } // highest_index_in (where

How does Java decide the type of a ternary conditional operator expression?

时光怂恿深爱的人放手 提交于 2020-01-04 04:00:50
问题 Can anyone explain this? public class Test { public static void main(String[] args) { char c = 'A'; int i = 0; boolean b = true; System.out.println(b ? c : i); System.out.println(b ? c : (char)i); System.out.println(b ? c : 0); System.out.println(b ? c : (char)0); } } Output: 65 A A A It sure looks strange from where I'm standing. I would have expected only A s to print out. And moreover: how come when I substitute 0 for i the output changes? The output seems to be the same for all values of

Is there a way to short-circuit a ternary operation?

孤人 提交于 2020-01-04 03:53:06
问题 I can write if(Model.DecisionReason != null && Model.DecisionReason.Length > 35) return Model.DecisionReason.Substring(0, 32) + "..."; else return Model.DecisionReason; and the && comparison in the if will short-circuit, preventing an exception if Model.DecisionReason is null. However, if I write return (Model.DecisionReason != null && Model.DecisionReason.Length > 35) ? Model.DecisionReason.Substring(0, 32) + "..." : Model.DecisionReason; There is no short-circuit and I hit the exception. Is

Type of ternary expression

泄露秘密 提交于 2020-01-03 06:44:06
问题 Can anyone explain the output of the following program: #include <iostream> using namespace std; int main() { int test = 0; cout << "First character " << '1' << endl; cout << "Second character " << (test ? 3 : '1') << endl; return 0; } Output: First character 1 Second character 49 But both the printf statements should print the same line. 回答1: The type of the expression '1' is char . The type of the expression (test ? 3 : '1') is at least int (or an unsigned version thereof; portably it is

What is the type of “auto var = {condition} ? 1 : 1.0” in C++11? Is it double or int?

廉价感情. 提交于 2020-01-02 02:22:10
问题 In C++11 what are the types of x and y when I write this? int main() { auto x = true ? 1 : 1.0; auto y = false ? 1 : 1.0; std::cout << x << endl; std::cout << y << endl; return 0; } 回答1: The type is going to be double , because it's the common type of the literals 1 and 1.0 . There's a simple way to test that using typeid : #include <iostream> #include <typeinfo> using namespace std; int main() { auto x = true ? 1 : 1.0; cout << typeid(x).name() << endl; return 0; } This outputs d on my

Assigning using ternary operator?

廉价感情. 提交于 2019-12-31 09:12:13
问题 I am on Perl 5.8 and am needing to assign a default value. I ended up doing this: if ($model->test) { $review = "1" } else { $review = '' } The value of $model->test is going to be either "1" or undefined. If there's something in $model->test , set $review to "1" otherwise set it equal to '' . Because it's not Perl 5.10 I can't use the new swanky defined-or operator. My first reaction was to use the ternary operator like this... defined($model->test) ? $review = "1" : $review = ''; but that

Why can't I use break in a C# ternary expression?

99封情书 提交于 2019-12-31 07:32:09
问题 I am trying to convert the if else clause to a ternary within a while loop, however it's not allowing me to have a break after the question mark, pointing an error at the break as an invalid expression. How would I go about turning this simple if else into a ternary like so. while (true) { Console.WriteLine("Enter 3 words seperated by spaces: "); var input = Console.ReadLine(); //input == "" ? break : ConvertToPascal(input); if (input == "") break; else ConvertToPascal(input); } } 回答1: It isn

Unexpected Result, Ternary Operator in Gnu C

六月ゝ 毕业季﹏ 提交于 2019-12-31 04:58:45
问题 So the operator precedence of the ternary operator in C seems truly bizarre to me. Case in point: #include <stdio.h> int main () { int i=5; int j=6; int k=7; printf("A: %d\n", i+j+(k!=7)?1:11); //prints 1 printf("B: %d\n", i+j+((k!=7)?1:11)); //prints 22 return 0; } This seems similar to the question here: C++ ternary conditional and assignment operator precedence Ternary operator evaluation order As a clarification, I understand that the parentheses make it work, as my comments in my

Unexpected Result, Ternary Operator in Gnu C

天涯浪子 提交于 2019-12-31 04:58:05
问题 So the operator precedence of the ternary operator in C seems truly bizarre to me. Case in point: #include <stdio.h> int main () { int i=5; int j=6; int k=7; printf("A: %d\n", i+j+(k!=7)?1:11); //prints 1 printf("B: %d\n", i+j+((k!=7)?1:11)); //prints 22 return 0; } This seems similar to the question here: C++ ternary conditional and assignment operator precedence Ternary operator evaluation order As a clarification, I understand that the parentheses make it work, as my comments in my

Conditional operator and Comparison Delegate

浪子不回头ぞ 提交于 2019-12-30 20:20:27
问题 Given two implementations of Comparison methods: // compares by Key... private static int CompareByKey(KeyValuePair<int, string> x, KeyValuePair<int, string> y) { return x.Key.CompareTo(y.Key); } // compares by Value... private static int CompareByValue(KeyValuePair<int, string> x, KeyValuePair<int, string> y) { return x.Value.CompareTo(y.Value); } Why wouldn't the following conditional operator code block compile: Comparison<KeyValuePair<int, string>> sortMethod; sortMethod =