operator-precedence

“x = ++x” is it really undefined?

蓝咒 提交于 2019-11-30 08:07:39
问题 I am using Coverity Prevent on a project to find errors. It reports an error for this expression (The variable names are of course changed): x= (a>= b) ? ++x: 0; The message is: EVALUATION_ORDER defect: In " x=(a>= b) ? ++x: 0; ", " x " is written in " x " (the assignment LHS) and written in " (a>= b) ? ++x: 0; " but the order in which the side effects take place is undefined because there is no intervening sequence point. END OF MESSAGE While I can understand that " x = x++ " is undefined,

How do you do many to many table outer joins?

好久不见. 提交于 2019-11-30 05:11:47
I have 3 tables, foo, foo2bar, and bar. foo2bar is a many to many map between foo and bar. Here are the contents. select * from foo +------+ | fid | +------+ | 1 | | 2 | | 3 | | 4 | +------+ select * from foo2bar +------+------+ | fid | bid | +------+------+ | 1 | 1 | | 1 | 2 | | 2 | 1 | | 2 | 3 | | 4 | 4 | +------+------+ select * from bar +------+-------+------+ | bid | value | zid | +------+-------+------+ | 1 | 2 | 10 | | 2 | 4 | 20 | | 3 | 8 | 30 | | 4 | 42 | 30 | +------+-------+------+ What I want to request is, "Give me a list of all the fid and values with zid of 30" I expect an

Why is && at a higher precedence than || (Java)

北战南征 提交于 2019-11-30 04:05:12
问题 boolean a = true; boolean b = true; boolean c = false; System.out.println(a || b && c); // true System.out.println(b && c || a); // true I just recently discovered what I thought was a bit of an oddity here. Why is it that && and || are at different precedence levels? I would have assumed that they were at the same level. The above demonstrates it. both statements are true even though a left to right evaluation would give false for the first and true for the second. Does anyone know the

Why does “new Date().toString()” work given Javascript operator precedence?

心已入冬 提交于 2019-11-30 02:57:40
MDN states that there are two operators in Javscript that share the highest precedence: The left-associative member operator: foo.bar The right-associative new operator: new Foo() I usually explicitly separate the two: (new Date()).toString() But I frequently see both of them combined: new Date().toString() According to this answer , the reason the second way works is that it's the second operator's associativity that matters when both operators have equal precedence. In this case, the member operator is left associative which means new Date() is evaluated first. However, if that's the case,

A strange operation problem in SQL Server (-100/-100*10 = 0)

不想你离开。 提交于 2019-11-29 20:13:37
If you execute SELECT -100/-100*10 the result is 0 . If you execute SELECT (-100/-100)*10 the result is 10 . If you execute SELECT -100/(-100*10) the result is 0 . If you execute SELECT 100/100*10 the result is 10 . BOL states: When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression. And Level Operators 1 ~ (Bitwise NOT) 2 * (Multiplication), / (Division), % (Modulus) 3 + (Positive), - (Negative), + (Addition), + (Concatenation), - (Subtraction), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise

Why in Python does 0, 0 == (0, 0) equal (0, False)

雨燕双飞 提交于 2019-11-29 19:29:09
In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well): (0, 0) == 0, 0 # results in a two element tuple: (False, 0) 0, 0 == (0, 0) # results in a two element tuple: (0, False) (0, 0) == (0, 0) # results in a boolean True But: a = 0, 0 b = (0, 0) a == b # results in a boolean True Why does the result differ between the two approaches? Does the equality operator handle tuples differently? The first two expressions both parse as tuples: (0, 0) == 0 (which is False ), followed by 0 0 , followed by 0 == (0, 0) (which is still False that way

“C - C++” joke about postfix/prefix operation ordering

自作多情 提交于 2019-11-29 18:52:23
My friend sent me a joke: Q. What's the difference between C and C++? A. Nothing, because: (C - C++ == 0) I tried to change order and got stuck. Look at this code: public class Test { public static void main(String args[]) { int c = 10; System.out.println(c++ - c); System.out.println(++c - c); } } Why does it return: -1 0 I understand postfix and prefix increment. Why isn't this the result? 0 1 Because in the first example, c starts out 10. c++ increments c and returns 10, so the second c now evaluates to 11 since it was incremented. So the ultimate expression evaluated is 10 - 11 , which

PHP7 method_exists Uncaught Error: Function name must be a string

我们两清 提交于 2019-11-29 17:33:58
问题 I am getting this error: Fatal error: Uncaught Error: Function name must be a string in For this code: if (function_exists($item['function'])) { $item['function']($item, $default); } elseif (method_exists($this, $item['function'])) { $this->$item['function']($item, $default); } I know that changing the code to if (function_exists($item['function'])) { $item['function']($item, $default); } elseif (method_exists($this,$item['function'])) { $this->{$item['function']}($item, $default); } Solved

What is run first inside a cout statement? (C++17)

自古美人都是妖i 提交于 2019-11-29 16:44:38
Say for example I have a long statement like cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n"; would findCurrent() be run before findLowest() like logic dictates? Since C++17 the functions are guaranteed to be called left-to-right, i.e. findCurrent() is called first, then findLowest() and so on. C++17 Standard references: [expr.shift]/4 (referring to the expression E1 << E2 ): The expression E1 is sequenced before the expression E2 . [over.match.oper]/2: (describing overloaded operators) the operands are sequenced in the order prescribed for the

C #define macros

て烟熏妆下的殇ゞ 提交于 2019-11-29 15:01:19
Here is what i have and I wonder how this works and what it actually does. #define NUM 5 #define FTIMES(x)(x*5) int main(void) { int j = 1; printf("%d %d\n", FTIMES(j+5), FTIMES((j+5))); } It produces two integers: 26 and 30. How does it do that? The reason this happens is because your macro expands the print to: printf("%d %d\n", j+5*5, (j+5)*5); Meaning: 1+5*5 and (1+5)*5 Since it hasn't been mentioned yet, the way to fix this problem is to do the following: #define FTIMES(x) ((x)*5) The parentheses around x in the macro expansion prevent the operator associativity problem. define is just a