comma-operator

JavaScript declare a variable and use the comma operator in one statement?

只谈情不闲聊 提交于 2020-04-11 05:45:25
问题 it's known that to declare multiple variables, one uses a format like: let k = 0, j = 5 /*etc....*/ It's also known that to execute multiple statements in one line (which is useful for arrow functions, making it not necessary to write the return keyword), the comma "," operator is also used, like so: let r = "hello there world, how are you?" .split("") .map(x => (x+=5000, x.split("").map( y => y+ + 8 ).join(""))) .join("") console.log(r) not the most elegant example, but the point is you can

The void(), the comma operator (operator,) and the impossible (?) overloading

感情迁移 提交于 2020-02-27 04:22:50
问题 Consider the following struct: struct S {}; In C++14, the definition below is valid: constexpr auto f() { return S{}, 'c'; } As well as the following one: constexpr auto f() { return S{}, void(); } Now, consider the following, working snippet that involves the first of the two definitions: #include<type_traits> struct S {}; constexpr int operator,(S, char) { return 42; } constexpr auto f() { return S{}, 'c'; } int main() { constexpr int i{f()}; static_assert(i == 42, "!"); static_assert(std:

The void(), the comma operator (operator,) and the impossible (?) overloading

落花浮王杯 提交于 2020-02-27 04:21:32
问题 Consider the following struct: struct S {}; In C++14, the definition below is valid: constexpr auto f() { return S{}, 'c'; } As well as the following one: constexpr auto f() { return S{}, void(); } Now, consider the following, working snippet that involves the first of the two definitions: #include<type_traits> struct S {}; constexpr int operator,(S, char) { return 42; } constexpr auto f() { return S{}, 'c'; } int main() { constexpr int i{f()}; static_assert(i == 42, "!"); static_assert(std:

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(