operator-precedence

Simple Java regex not working

坚强是说给别人听的谎言 提交于 2019-12-08 19:26:52
问题 I have this regex which is supposed to remove sentence delimiters( . and ? ): sentence = sentence.replaceAll("\\.|\\?$",""); It works fine it converts "I am Java developer." to "I am Java developer" "Am I a Java developer?" to "Am I a Java developer" But after deployment we found that it also replaces any other dots in the sentence as "Hi.Am I a Java developer?" becomes "HiAm I a Java developer" Why is this happening? 回答1: The pipe ( | ) has the lowest precedence of all operators. So your

Why the order of evaluation for expressions involving user variables is undefined?

有些话、适合烂在心里 提交于 2019-12-08 13:11:33
问题 From MySQL Manual the output of the following query is not guaranteed to be same always. SET @a := 0; SELECT @a AS first, @a := @a + 1 AS second, @a := @a + 1 AS third, @a := @a + 1 AS fourth, @a := @a + 1 AS fifth, @a := @a + 1 AS sixth; Output: first second third fourth fifth sixth 0 1 2 3 4 5 Quoting from the Manual: However,the order of evaluation for expressions involving user variables is undefined; I want to know the story behind. So my question is : Why the order of evaluation for

Make an implicit conversion operator preferred over another in C++

社会主义新天地 提交于 2019-12-08 03:55:03
问题 I would like to prefer a certain implicit conversion sequence over another. I have the following (greatly simplified) class and functions: class Whatever {...} template <class T> class ref { public: operator T* () { return object; } operator T& () { return *object; } T* object; ... }; void f (Whatever*) { cout << "f (Whatever*)" << endl; } void f (Whatever&) { cout << "f (Whatever&") << endl; } int main (int argc, char* argv[]) { ref<Whatever> whatever = ...; f(whatever); } When I have a ref

output of expression in (--i + ++i) in java

半腔热情 提交于 2019-12-07 14:12:02
问题 int i=9; System.out.println(--i + ++i); output on execution : 17 The final value of i is : 9 But according to associativity and precedence rules in java,, ++i should be executed first i.e from Right to left which gives 10 and then --i gives 9 .. adding both,, the answer should be 19... As far as i have known such a code gives undefined behaviour in C/C++ but in java ,, the rules are strictly defined and there is no concept of sequence points. So, can anyone clarify the problem as iam really

Bitwise operator predence

梦想的初衷 提交于 2019-12-07 12:35:38
问题 A gotcha I've run into a few times in C-like languages is this: original | included & ~excluded // BAD Due to precedence, this parses as: original | (included & ~excluded) // '~excluded' has no effect Does anyone know what was behind the original design decision of three separate precedence levels for bitwise operators? More importantly, do you agree with the decision, and why? 回答1: The operators have had this precedence since at least C. I agree with the order because it is the same relative

Why are the 'dereference' and the 'address of' operators on the left?

别来无恙 提交于 2019-12-07 06:36:59
问题 In C (and some other C-like languages) we have 2 unary operators for working with pointers: the dereference operator ( * ) and the 'address of' operator ( & ). They are left unary operators, which introduces an uncertainty in order of operations, for example: *ptr->field or *arr[id] The order of operations is strictly defined by the standard, but from a human perspective, it is confusing. If the * operator was a right unary operator, the order would be obvious and wouldn't require extra

C# increment ToString

假如想象 提交于 2019-12-07 05:58:24
问题 I add an unexpected behaviour from C#/WPF private void ButtonUp_Click(object sender, RoutedEventArgs e) { int quant; if( int.TryParse(Qnt.Text, out quant)) { string s = ((quant++).ToString()); Qnt.Text = s; } } So, if I get quant as 1, quant will be incremented to 2. But the s string will be 1. Is this a question of precedence? EDIT: I re-wrote this as: quant++; Qnt.Text = quant.ToString(); and now this works as I expected. 回答1: You are using the post -increment operator. This evalutates to

why does *p++ = *p - a give strange results?

帅比萌擦擦* 提交于 2019-12-07 02:07:24
问题 While working with large arrays, I am doing unsafe pointer computations like the following: *c++ = *a++ - *b++; It works as expected. But for inplace operations, I need the c pointer on the right side as well: [STAThread] unsafe static void Main(string[] args) { double[] arr = new double[] { 2, 4, 6, 8, 10 }; double scalar = 1; fixed (double* arrP = arr) { double* end = arrP + arr.Length; double* p = arrP; double* p2 = arrP; while (p < end) { // gives: 3,5,7,9,2,4827634676971E+209 *p++ = *p -

c# generic method overload not consistent with abstract Visitor pattern

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 01:56:55
问题 experimenting with Visitor pattern and generic method I found a kind of discrepancy in C#.NET. AFAIK C# compiler prefers an explicit overload to a generic method, therefore the following code: public abstract class A { public abstract void Accept(Visitor v); } public class B : A { public override void Accept(Visitor v) { v.Visit(this); } } public class C : A { public override void Accept(Visitor v) { v.Visit(this); } } public class D : A { public override void Accept(Visitor v) { v.Visit(this

Is a C++ optimizer allowed to move statements across a function call?

人盡茶涼 提交于 2019-12-06 18:09:37
问题 Note: No multithreading at all here. Just optimized single-threaded code. A function call introduces a sequence point. (Apparently.) Does it follow that a compiler (if the optimizer inlines the function) is not allowed to move/intermingle any instructions prior/after with the function's instructions? (As long as it can "proove" no observable effects obviously.) Explanatory background: Now, there is a nice article wrt. a benchmarking class for C++, where the author stated: The code we time won