ternary-operator

Is the ternary operator (?:) thread safe in C#?

纵饮孤独 提交于 2019-12-05 04:45:34
Consider the following two alternatives of getting the higher number between currentPrice and 100 ... int price = currentPrice > 100 ? currentPrice : 100 int price = Math.Max(currentPrice, 100) I raised this question because I was thinking about a context where the currentPrice variable could be edited by other threads. In the first case... could price obtain a value lower than 100 ? I'm thinking about the following: if (currentPrice > 100) { //currentPrice is edited here. price = currentPrice; } It is not threadsafe. ?: is just shortcut for normal if , so your if sample is equivalent to ? one

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

核能气质少年 提交于 2019-12-05 03:23:31
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; } 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 version of GCC. Running echo d | c++filt -t then tells us that d corresponds to the type double , as expected.

C++, ternary operator, std::cout

主宰稳场 提交于 2019-12-05 00:40:25
How to write the following condition with a ternary operator using C++ int condition1, condition2, condition3; int / double result; //int or double .... std::cout << ( condition1: result1 : "Error" ) << ( condition2: result2 : "Error" ) << ( condition3: result3 : "Error")...; Armen Tsirunyan Depends on what type is result1, result2 etc. expressionC ? expression1 : expression2 isn't valid for all types of expression1 and expression2 . They must necessarily be convertible to a common type, roughly speaking (exact rules and exceptions can be read in the standard). Now, if result s are strings,

Does VHDL have a ternary operator?

为君一笑 提交于 2019-12-05 00:21:40
I love the neatness of the ternary operator vs if clauses. Does this operator exist in vhdl? My search was to the contrary. I also checked the when statement out, but it's not an operator, and I want to be able to use it in processes, too... No. It was discussed for VHDL-2008, but didn't get in. You've got a couple of options. If your tools support VHDL-2008, conditional assignments are now supported as sequential statements (they were previously just concurrent), so you can write something like: process(clock) begin if rising_edge(clock) then q <= '0' when reset else d; -- ie. much like q <=

Delphi - Equivalent to C#'s ternary operator? [duplicate]

白昼怎懂夜的黑 提交于 2019-12-04 23:47:26
This question already has answers here : Closed 8 years ago . Possible Duplicate: Is there, or is there ever going to be, a conditional operator in Delphi? I understand Delphi doesnt have the ternary operator as in C#. i.e. ?: So how best to represent this function call? Whats the cleanest method out there? Would be very nice if there is any code out there that can be used INSTEAD of writing a separate function? If not, whats the most efficient and/or cleanest code representation of it? Of course you can use IfThen(SomeBooleanExpression, IfTrueReturnValue, IfFalseReturnValue) where the return

Ternary Operator syntax to choose implementation of Interface [duplicate]

蓝咒 提交于 2019-12-04 23:27:54
This question already has an answer here: Implicit conversion issue in a ternary condition [duplicate] 4 answers I am wondering why this line of code doesn't compile: ILogStuff Logger = (_logMode) ? new LogToDisc() : new LogToConsole(); Note that both classes LogToDisc and LogToConsole implement ILogStuff , and _logMode is a boolean variable. The error message I get is: Error 3: Type of conditional expression cannot be determined because there is no implicit conversion between 'xxx.LogToDisc' and 'xxx.LogToConsole' But why should there be one? What am I missing? Adil There is not implicit

Ternary operator in CMake's generator expressions

∥☆過路亽.° 提交于 2019-12-04 22:33:55
Cmake's generator expressions allow me to use logical expressions within certain function calls. For instance, if I want to add the /MTd compiler flag in Debug mode, I can say add_compile_options($<$<CONFIG:Debug>:/MTd>) If CONFIG equals "Debug", this will call add_compile_options with the value "/MTd", otherwise with an empty string. But usually, I don't want to decide between a value and the empty string, but between two values. In the example above, if CONFIG is not "Debug", I want to pass /MT (without the trailing d). I'd love to have a syntax like this: add_compile_options($<$<CONFIG

Is there a difference between a ternary operator and an if statement in C#? [duplicate]

随声附和 提交于 2019-12-04 13:49:10
This question already has an answer here: Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate] 9 answers Conditional operator cannot cast implicitly? 3 answers I'm working with a nullable DateTime object and ran into some strange behavior. Here's a sample function: public DateTime? Weird() { DateTime check = DateTime.Now; DateTime? dt; if (check == DateTime.MinValue) dt = null; else dt = Viewer.ActiveThroughUTC.ToLocalTime(); //this line give a compile error dt = (check == DateTime.MinValue) ? (null) : (Viewer.ActiveThroughUTC.ToLocalTime()); return dt; } As far

Java Ternary Operator inside ternary operator, how evaluated?

别来无恙 提交于 2019-12-04 11:10:17
Very basic question I suppose, I just wanted to know how this code is read: return someboolean ? new someinstanceofsomething() : someotherboolean ? new otherinstance() : new third instance(); I guess now as I'm writing it I kind of understand the statement. It returns option one if true but then does another boolean check if false and returns one of the two remaining options? I'm going to continue to leave this question because I have not seen it before and maybe others have not as well. Could you go on indefinitely with ternary inside of ternary operations? Edit: Also why is this/is this not

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

99封情书 提交于 2019-12-04 10:40:48
问题 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? 回答1: 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