conditional-operator

null coalesce operator in VB.Net(8)

◇◆丶佛笑我妖孽 提交于 2019-12-10 10:05:32
问题 i'm afraid that this is a stupid question, but i must assume that i have programmed VB.Net too long and now can't figure out how to convert this C# null coalescing operator into VB.Net: if( Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] ?? true) == false ){} I know the IIF-Function but i'm not sure how to use it here and if it gives the correct result(in IIF both expressions are being evaluated). Please help to shed light on the dark. EDIT : if you want to see the source of this:

Does the ternary operator short circuit in a defined way

浪子不回头ぞ 提交于 2019-12-08 16:14:34
问题 If you have the following: if (x) { y = *x; } else { y = 0; } Then behavior is guaranteed to be defined since we can only dereference x if it is not 0 Can the same be said for: y = (x) ? *x : 0; This seems to work as expected (even compiled with -Wpedantic on g++) Is this guaranteed? 回答1: Yes, only the second or third operand will be evaluated, the draft C++ standard section 5.16 [expr.cond] says: Conditional expressions group right-to-left. The first expression is contextually converted to

Can the conditional operator lead to less efficient code?

不想你离开。 提交于 2019-12-08 16:05:33
问题 Can ?: lead to less efficient code compared to if/else when returning an object? Foo if_else() { if (bla) return Foo(); else return something_convertible_to_Foo; } If bla is false, the returned Foo is directly constructed from something_convertible_to_Foo . Foo question_mark_colon() { return (bla) ? Foo() : something_convertible_to_Foo; } Here, the type of the expression after the return is Foo , so I guess first some temporary Foo is created if bla is false to yield the result of the

Using ternary operator on Console.WriteLine

亡梦爱人 提交于 2019-12-08 02:25:51
问题 I need to print some string based on the true or false of a condition. For example: if(i == m) { Console.WriteLine("Number is valid"); } else { Console.WriteLine("Number is invalid"); } How can I check this condition and print a message using conditional operator and with only one Console.WriteLine ? I was trying: (i == m) ? Console.WriteLine("Number is valid") : Console.WriteLine("Number is not valid"); I know I'm doing it wrong here. Can someone please tell me the correct way? 回答1: Try this

Speed of if compared to conditional

孤街浪徒 提交于 2019-12-07 19:18:44
问题 I had the idea I would turn some of my if blocks into single lines, using the conditional operator. However, I was wondering if there would be a speed discrepancy. I ran the following test: static long startTime; static long elapsedTime; static String s; public static void main(String[] args) { startTime = System.nanoTime(); s = ""; for (int i= 0; i < 1000000000; i++) { if (s.equals("")) { s = ""; } } elapsedTime = System.nanoTime() - startTime; System.out.println("Type 1 took this long: " +

Conditional operator can't resolve overloaded member function pointers

岁酱吖の 提交于 2019-12-07 05:35:37
问题 I'm having a minor issue dealing with pointers to overloaded member functions in C++. The following code compiles fine: class Foo { public: float X() const; void X(const float x); float Y() const; void Y(const float y); }; void (Foo::*func)(const float) = &Foo::X; But this doesn't compile (the compiler complains that the overloads are ambiguous): void (Foo::*func)(const float) = (someCondition ? &Foo::X : &Foo::Y); Presumably this is something to do with the compiler sorting out the return

Conditional function in APL

戏子无情 提交于 2019-12-07 00:45:07
问题 Is there a symbol or well-known idiom for the conditional function, in any of the APL dialects? I'm sure I'm missing something, because it's such a basic language element. In other languages it's called conditional operator , but I will avoid that term here, because an APL operator is something else entirely. For example C and friends have x ? T : F LISPs have (if x T F) Python has T if x else F and so on. I know modern APLs have :If and friends, but they are imperative statements to control

ternary operator doesn't work with lambda functions

拜拜、爱过 提交于 2019-12-06 18:16:36
问题 I am assigning to a std::function<double()> a lambda expression. This snippet works if(fn_type==exponential) k.*variable = [=,&k](){ return initial*exp(-k.kstep*par); }; else k.*variable = [=,&k](){ return initial*pow(k.kstep, par); }; whereas if I want to use the ternary operator k.*variable = (fn_type==exponential ? [=,&k](){ return initial*exp(-k.kstep*par); } : [=,&k](){ return initial*pow(k.kstep, par); }); I get the following error: error: no match for ternary ‘operator?:’ in <awfully

Using ternary operator on Console.WriteLine

别来无恙 提交于 2019-12-06 11:57:27
I need to print some string based on the true or false of a condition. For example: if(i == m) { Console.WriteLine("Number is valid"); } else { Console.WriteLine("Number is invalid"); } How can I check this condition and print a message using conditional operator and with only one Console.WriteLine ? I was trying: (i == m) ? Console.WriteLine("Number is valid") : Console.WriteLine("Number is not valid"); I know I'm doing it wrong here. Can someone please tell me the correct way? Try this: Console.WriteLine("Number is " + ((i == m) ? "valid" : "not valid")); Move your ternary operation inside

How can an array work with the conditional operator?

回眸只為那壹抹淺笑 提交于 2019-12-06 02:43:35
This is a retelling of my previous post , since I changed the question (so it probably didn't get flagged as a new question and was missed). I'll hopefully trim it down too. I had functions like: #include <cstddef> #include <type_traits> template < typename E, typename T > inline constexpr auto checked_slice( E &&, T &&t ) noexcept -> T && { return static_cast<T &&>(t); } template < typename E, typename T, std::size_t N, typename U, typename ...V > inline constexpr auto checked_slice( E &&e, T (&t)[N], U &&u, V &&...v ) -> typename remove_some_extents<T, sizeof...(V)>::type & { typedef