comma-operator

Disallow using comma operator

…衆ロ難τιáo~ 提交于 2019-12-23 08:00:39
问题 I never use the comma operator. But sometimes, when I write some recursions, I make a stupid mistake: I forget the function name. That's why the last operand is returned, not the result of a recursion call. Simplified example: int binpow(int a,int b){ if(!b) return 1; if(b&1) return a*binpow(a,b-1); return (a*a,b/2); // comma operator } Is it possible get a compilation error instead of incorrect, hard to debug code? 回答1: Yes, with a caveat. The gcc has the -Wunused-value warning (or error

Destructor call in a comma-separated expression

早过忘川 提交于 2019-12-21 10:16:34
问题 consider the following example program: #include <iostream> using namespace std; struct t { ~t() {cout << "destroyed\n"; } }; int main() { cout << "test\n"; t(), cout << "doing stuff\n"; cout << "end\n"; } The output I get with GCC 4.9.2 is: test doing stuff destroyed end cpp.sh link: http://cpp.sh/3cvm However according to cppreference about the comma operator: In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before

Destructor call in a comma-separated expression

十年热恋 提交于 2019-12-21 10:16:18
问题 consider the following example program: #include <iostream> using namespace std; struct t { ~t() {cout << "destroyed\n"; } }; int main() { cout << "test\n"; t(), cout << "doing stuff\n"; cout << "end\n"; } The output I get with GCC 4.9.2 is: test doing stuff destroyed end cpp.sh link: http://cpp.sh/3cvm However according to cppreference about the comma operator: In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before

Different behaviour of comma operator in C++ with return?

感情迁移 提交于 2019-12-20 09:25:59
问题 This (note the comma operator ): #include <iostream> int main() { int x; x = 2, 3; std::cout << x << "\n"; return 0; } outputs 2 . However, if you use return with the comma operator, this: #include <iostream> int f() { return 2, 3; } int main() { int x; x = f(); std::cout << x << "\n"; return 0; } outputs 3 . Why is the comma operator behaving differently with return ? 回答1: According to the Operator Precedence, comma operator has lower precedence than operator= , so x = 2,3; is equivalent to

Move constructor suppressed by comma operator

老子叫甜甜 提交于 2019-12-19 12:51:15
问题 This program: #include <iostream> struct T { T() {} T(const T &) { std::cout << "copy constructor "; } T(T &&) { std::cout << "move constructor "; } }; int main() { ([](T t) -> T { return t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), t; })({}); std::cout << '\n'; ([](T t) -> T { return void(), std::move(t); })({}); std::cout << '\n'; } when compiled by gcc-4.7.1 outputs (link): move constructor copy constructor move constructor Why does the comma operator have this effect? The

Java - comma operator outside for loop declaration

此生再无相见时 提交于 2019-12-19 02:47:05
问题 I know I can use the comma operator like this for (int i = 1, j = 15; j>10; i++, j--) { // do something neat } but some articles seem to suggest that the comma operator can be used outside of the for loop declaration, for example int j = 2, k = 4 ; int x ; // Assignment statement with comma operator x = j + 1, k ; source: http://www.cs.umd.edu/~clin/MoreJava/ControlFlow/comma.html or int x = (expression) ? (i++,2) : 3; source: https://stackoverflow.com/a/12047433/1084813 This would be a neat

Comma operator in c [duplicate]

一曲冷凌霜 提交于 2019-12-18 18:33:28
问题 This question already has answers here : What does the comma operator , do? (8 answers) Closed 2 years ago . #include<stdio.h> int main(void) { int a=(1, 2), 3; printf("%d", a); return 0; } output: 2 Can any one explain how output is 2? 回答1: Can any one explain how output is 2? In the statement a = (1, 2), 3; , used is a comma operator. Due to higher operator precedence of = operator than that of , operator, the expression operand (1, 2) will bind to = as (a = (1, 2)), 3; In case of comma

C++ overloading operator comma for variadic arguments

孤街醉人 提交于 2019-12-18 16:54:44
问题 is it possible to construct variadic arguments for function by overloading operator comma of the argument? i want to see an example how to do so.., maybe something like this: template <typename T> class ArgList { public: ArgList(const T& a); ArgList<T>& operator,(const T& a,const T& b); } //declaration void myFunction(ArgList<int> list); //in use: myFunction(1,2,3,4); //or maybe: myFunction(ArgList<int>(1),2,3,4); 回答1: It is sort-of possible, but the usage won't look very nice. For exxample:

How to directly assign complex numbers to a variable?

こ雲淡風輕ζ 提交于 2019-12-18 13:22:11
问题 Using the complex class and library, how do I assign a complex number to a variable? I understand that I can set the value when I first instantiate the complex number. I also understand that I can assign one instantiated complex number to another. How can I directly assign a complex number to a variable? Reference: http://www.cplusplus.com/reference/complex/complex/operators/ Example: #include <iostream> #include <complex> int main() { complex<double> a(1.2,3.4), b; cout << a; //-> (1.2,3.4)

Is the comma operator allowed in a constant-expression in C++11?

耗尽温柔 提交于 2019-12-18 12:47:49
问题 In the process of answering this question on SO for C++11, I realized that in C++03 (as well as in C) the use of the comma operator is explicitly forbidden in a constant-expression . Paragraph 5.19/1 of the C++03 Standard on constant expressions says: [...] In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used . In C++11, however, that last part