comma-operator


C-preprocessor: iteratively expand macro to comma-separated list

别说谁变了你拦得住时间么 提交于 2020-01-13 10:44:11
问题 Using Paul Fultz II's solution in the post C-preprocessor recursive macro, I'd like to expand an unlimited number of parenthesized macro arguments, e.g. #define MY_CHAIN (alpha) (beta) (gamma) into a comma-separated list which can be passed to a variadic macro, e.g. CHAIN_COMMA(MY_CHAIN) // alpha, beta, gamma I'm able to expand into braces [alpha] [beta] [gamma] and delimit the list with everything I've tried except a comma, alpha :: beta :: gamma in the example below. Here is my full

sizeof taking two arguments

好久不见. 提交于 2020-01-09 06:20:09
问题 In C.1.3 of the C++ IS (2003. It's in the C++11 IS, too), the standard points out a difference between ISO C and C++; namely, for char arr[100]; sizeof(0, arr) returns sizeof(char*) in C, but 100 in C++. I can find no documentation for sizeof taking two arguments. The obvious fallback is the comma operator, but I don't think so: sizeof(arr) in C is 100 ; sizeof(0, arr) is sizeof(char*) . Both sizeof(0, arr) and sizeof(arr) are 100 in C++. I may be missing the whole point of the IS in this

Should a diagnostic be emmited for discarded value expressions that do not have side effects?

六眼飞鱼酱① 提交于 2020-01-04 05:23:24
问题 After quite some debugging time I felt silly to discover a goof in my code that boils down to something like this : int main() { double p1[] = { 1, 2, 3 }; double p2[] = { 1, 2, 3 }; int color = 1; bool some_condition = true; if (some_condition) (p1, p2, color); } The (p1, p2, color) expression evaluates to its last operant, but should the compiler protect me in some way ? (Visual Studio told nothing) And yes you guessed it right, I wanted to call a draw function : Draw(p1, p2, color) 回答1: In

Smart pointer test in a while loop: use the comma operator?

为君一笑 提交于 2019-12-25 04:57:27
问题 I recently saw code like this: // 3rd Party API: (paraphrased) void APIResetIterator(int ID); // reset iterator for call to next() Mogrifier* APINext(int ID); // User must delete pointer returned ... typedef std::unique_ptr<Mogrifier> MogPtr; ... const it listID = 42; APIResetIterator(listID); MogPtr elem; while (elem.reset(APINext(listID)), elem) { // use elem } Is this a good idea? Does it work? I'll add the corresponding for loop for easy reference: for (MogPtr elem(APINext(listID)); elem;

Proper use of a comma in javascript ternary operator

给你一囗甜甜゛ 提交于 2019-12-24 01:44:08
问题 Rather than use an if else statement, I'm trying to use the ternary operator but have a syntax error somewhere in my statement. Can someone tell me where I am going wrong? Statement is: my_alert(status ? ('Accepted', 'alert-success') : ('Declined', 'alert-info')) my_alert is a function which has 2 parameters. Status just evaluates to true or false. When I pass more than 1 parameter into the above expression, it doesn't like the use of the comma. In chrome and firefox when the function runs it

comma operator and comma seperator in c++ [duplicate]

倖福魔咒の 提交于 2019-12-23 18:33:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When all does comma operator not act as a comma operator? when does comma(,) behave as operator and when does it behave as separator?And what are the consequences of it.If possible please give small examples too for both. 回答1: The comma behaves as a separator in function calls, function declarations, initializers and variable declarations: f(a, b); int a[] = {2, 3, 4}; int c = 2, d = 3; By contrast, when used to

AS3/JavaScript if statement commas instead of & &

家住魔仙堡 提交于 2019-12-23 15:15:10
问题 This runs in ActionScript 3 and JavaScript. Why? I know how && and || work, but a list? Is this AS3 specific? Is this in other languages? I'm a mouth breathing PHP/AS2 programmer. Or did everyone already know this and I'm a tool for not reading documentation properly? AS3 if (true, true, true) { trace("true?") } //result - "true?" traced JavaScript if (true, true, true) { alert("true?"); } //result - "true?" alert message popped up if (false, false, false) { alert("true?"); } else { alert(

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

工具导航Map