comma-operator

When all does comma operator not act as a comma operator?

余生颓废 提交于 2019-11-29 13:34:04
If you see this code, class A{ public: A(int a):var(a){} int var; }; int f(A obj) { return obj.var; } int main() { //std::cout<<f(23); // output: 23 std::cout<<f(23, 23); // error: too many arguments to function 'int f(A)' return 0; } f(23, 23) does not compile because the comma acts as a separator here and not as a comma operator. Where all does a comma not work as a comma operator? Or the other way around? CB Bailey From a grammatical point of view, the parameters of a function call form an optional expression-list inside parentheses. An expression-list consists of one or more assignment

Javascript “tuple” notation: what is its point?

梦想与她 提交于 2019-11-28 21:04:42
问题 At wtfjs, I found that the following is legal javascript. ",,," == Array((null,'cool',false,NaN,4)); // true The argument (null,'cool',false,NaN,4) looks like a tuple to me, but javascript does not have tuples! Some quick tests in my javascript console yields the following. var t = (null,'cool',false,NaN,4); // t = 4 (null,'cool',false,NaN,4) === 4; // true (alert('hello'), 42); // shows the alert and returns 42 It appears to behave exactly like a semicolon ; separated list of statements,

Comma Operator with indexing 2D arrays

痞子三分冷 提交于 2019-11-28 11:58:20
问题 I have this algorithm that is pseudocode for the dijkstra algorithm for graph theory. The first thing that goes on is a basic for loop. visitedSet[0] = true //visitedSet is a array of bools for (int i = 1; i <= numberNodes; ++i) { distanceArray[i] = adjacencyMatrix[0,i]; //distanceArray is 1D with size of fifty //adjacencyMatrix is 2D with size of fifty //Both arrays hold values of unsigned ints } Here are the array definitions enum GraphLimit300 {MAX_NODES = 50}; unsigned int adjacencyMatrix

How is the comma operator being used here? [duplicate]

隐身守侯 提交于 2019-11-28 11:36:34
Possible Duplicate: C++ Comma Operator Uses of C comma operator I am not new to C++, but this is the first time I see the following code: int a=0; int b=(a=2,a+1); That is C++ code. Can you tell me what is going on here? And how variable b gets value 3? This code is equivalent to this: int a = 2 ; int b = a + 1 ; First expression to the left of comma gets evaluated, then the one to its right. The result of the right most expression is stored in the variable to the left of = sign. Look up the comma operator for more details. http://en.wikipedia.org/wiki/Comma_operator (a = 2, a + 1); return 3

Comma-Separated return arguments in C function [duplicate]

假装没事ソ 提交于 2019-11-28 08:12:22
问题 This question already has answers here : What does the comma operator , do? (8 answers) Closed 4 years ago . While completing a C programming test, I was given a question regarding the expected output from a function which seem to return two values. It was structured as follows: int multi_return_args(void) { return (44,66); } The question caught me by surprise and inherently thought that if possible the first argument would be passed to the caller. But after compiling it, the result is 66

When all does comma operator not act as a comma operator?

时光怂恿深爱的人放手 提交于 2019-11-28 07:25:31
问题 If you see this code, class A{ public: A(int a):var(a){} int var; }; int f(A obj) { return obj.var; } int main() { //std::cout<<f(23); // output: 23 std::cout<<f(23, 23); // error: too many arguments to function 'int f(A)' return 0; } f(23, 23) does not compile because the comma acts as a separator here and not as a comma operator. Where all does a comma not work as a comma operator? Or the other way around? 回答1: From a grammatical point of view, the parameters of a function call form an

return list of values between parenthesis (10, 20, 30, 40)?

[亡魂溺海] 提交于 2019-11-28 00:19:38
I am working in C++ (not C++/CLI) in Visual Studio 2012 . I don't understand why this code works, I would have expected it to fail at compilation time, but it doesn't even fail at runtime: double MyClass::MyMethod() const { //some code here return (10, 20, 30, 40); } I produced this code by mistake, wasn't on purpose, I noticed the mistake when I was running my Unit Tests. And I am surprised it works. When I run it, it returns 40 , the last number on the list. Can someone explain me what this syntax means and why it works? Shafik Yaghmour This is using the comma operator which will evaluate

How does the compiler know that the comma in a function call is not a comma operator?

只谈情不闲聊 提交于 2019-11-27 22:40:03
Consider the function call (calling int sum(int, int) ) printf("%d", sum(a,b)); How does the compiler decide that the , used in the function call sum(int, int) is not a comma operator? NOTE : I didn't want to actually use the comma operator in the function call. I just wanted to know how the compiler knows that it is not a comma operator. ams Look at the grammar for the C language. It's listed, in full, in Appendix A of the standard . The way it works is that you can step through each token in a C program and match them up with the next item in the grammar. At each step you have only a limited

C++ — return x,y; What is the point?

岁酱吖の 提交于 2019-11-27 21:46:53
I have been programming in C and C++ for a few years and now I'm just now taking a college course in it and our book had a function like this for an example: int foo(){ int x=0; int y=20; return x,y; //y is always returned } I have never seen such syntax. In fact, I have never seen the , operator used outside of parameter lists. If y is always returned though, then what is the point? Is there a case where a return statement would need to be created like this? (Also, I tagged C as well because it applies to both, though my book specifically is C++) Joel The comma operator is primarily used in

What does the comma operator do?

本秂侑毒 提交于 2019-11-27 20:23:09
What does the following code do in C/C++? if (blah(), 5) { //do something } itsmatt Comma operator is applied and the value 5 is used to determine the conditional's true/false. It will execute blah() and get something back (presumably), then the comma operator is employed and 5 will be the only thing that is used to determine the true/false value for the expression. Note that the , operator could be overloaded for the return type of the blah() function (which wasn't specified), making the result non-obvious. If the comma operator is not overloaded, the code is similar to this: blah(); if (5) {