Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » What does , means?
What does , means? [message #15130] |
Wed, 02 April 2008 02:05 |
mobilehunter
Messages: 87 Registered: November 2006
|
Member |
|
|
Hi, please tell me what does ',' mean in this line:
lfnt.lfWeight = font.GetWidth(), angle, angle, font.IsBold() ? FW_BOLD : FW_NORMAL;
Thanks
|
|
|
Re: What does , means? [message #15131 is a reply to message #15130] |
Wed, 02 April 2008 09:56 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
http://msdn2.microsoft.com/en-us/library/zs06xbxh.aspx
It works similar to ";", but the whole sequence up to ";" is considered as single expression to compiler.
(handy especially in for statements, where you can do increment of two variables like this for ( ... ; ... ; ++var1, ++var2 ) ... but don't overuse it, it decreases the readability of code)
That said the "angle, angle, font.IsBold() ? FW_BOLD : FW_NORMAL" part looks useless to me, as it will produce 3 values (2x "angle" value, and once FW_BOLD or FW_NORMAL), which are not assigned anywhere, what makes them useless.
So you either put that line out of context and it is in reality parsed differently, or you have some very weird piece of code.
|
|
|
|
|
Re: What does , means? [message #15136 is a reply to message #15134] |
Wed, 02 April 2008 14:24 |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
Its a copy and paste error that luckily doesn't do anything:
lfnt.lfWeight = font.GetWidth(), angle, angle, font.IsBold() ? FW_BOLD : FW_NORMAL;
lfnt.lfItalic = font.IsItalic();
lfnt.lfUnderline = font.IsUnderline();
lfnt.lfStrikeOut = font.IsStrikeout();
wcscpy(lfnt.lfFaceName, ToSystemCharset(font.GetFaceName()));
f->hfont = CreateFontIndirect(&lfnt);
#else
f->hfont = CreateFont(font.GetHeight() ? -abs(font.GetHeight()) : -12,
font.GetWidth(), angle, angle, font.IsBold() ? FW_BOLD : FW_NORMAL,
You can see where it was copied from.
|
|
|
|
|
Re: What does , means? [message #15144 is a reply to message #15142] |
Wed, 02 April 2008 17:39 |
cbpporter
Messages: 1406 Registered: September 2007
|
Ultimate Contributor |
|
|
Quote: | Once again I'm not "guru enough" to know whether comma can be overloaded (but why not, looks reasonable ... except it makes me feel sick when I imagine it ).
|
I had to look it up also, and yes, it can be done. But I never did this myself. I couldn't and can't find a good reason to do this, not even as an alternative to cout << . Especially since arguments can be evaluated more than once. But still it is not totally improbable that Mirek used some overloading magic and constructed some font info structure with the help of such a statement. Anyway, without a file or line number, and judging by the fact that it was posted in the general C++ section, I had no way to be sure if it's from U++ and where to find it, so I couldn't look it up to see that it was overloaded or not. And fortunately, It wasn't .
|
|
|
|
Re: What does , means? [message #15149 is a reply to message #15130] |
Thu, 03 April 2008 00:27 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
The problem is with your original post.
That line in general C/C++ can mean pretty much anything.
Well, it's not that bad in C, there's only that preprocessor thing, but it's total havoc in C++ where you can also overload comma operator.
Unless you have full C++ source, ideally preprocessed already (-E switch for GCC), it's impossible to tell what exactly some line does and how it will end after compilation.
It can be eventually preceded by end-of-line comment which ends with backslash by accident, which will make it part of that comment! I have seen it all(?). (feeling old)
UPP does use overloaded operators aggressively (like <<= for callbacks and generally to assign value) to get "nicer and cleaner" source, but it makes the source somewhat confusing to pure C++ programmer who didn't study UPP basics. (but any C++ source can be confusing even without UPP magic, just check that link to IOCCC if you don't have idea how bad it can be)
So while the simple guess it's just ordinary comma with useless code (common copy/paste bug, luckily harmless this time) was right, it could have been worser, if someone would deliberately tried to obfuscate it with overloaded comma function and some macros for example with name "angle". Than the correct answer would require to see the definitions of those other things.
There's no other special UPP context about this, it was common *sigh* C++ code.
(anyway, it was very good question, keep asking whenever you don't fully understand some piece of UPP code... you will either learn something new, or help Mirek to find new bug )
|
|
|
|
Re: What does , means? [message #15269 is a reply to message #15144] |
Mon, 14 April 2008 16:50 |
|
tvanriper
Messages: 85 Registered: September 2007 Location: Germantown, MD, USA
|
Member |
|
|
cbpporter wrote on Wed, 02 April 2008 11:39 |
Quote: | Once again I'm not "guru enough" to know whether comma can be overloaded (but why not, looks reasonable ... except it makes me feel sick when I imagine it ).
|
I had to look it up also, and yes, it can be done. But I never did this myself. I couldn't and can't find a good reason to do this, not even as an alternative to cout << . Especially since arguments can be evaluated more than once.
|
In the boost library, they made use of overloading this operator to help make code more legible in their Assign library.
Give this a read and judge for yourself:
http://boost.org/doc/libs/1_35_0/libs/assign/doc/index.html
|
|
|
|
|
Re: What does , means? [message #15388 is a reply to message #15130] |
Fri, 18 April 2008 22:47 |
|
tvanriper
Messages: 85 Registered: September 2007 Location: Germantown, MD, USA
|
Member |
|
|
For lists filled algorithmically, yeah, this doesn't really provide much utility.
However, for static lists of specialized types, this bit of syntactic sugar can clarify your intentions quite nicely, and avoid having to type in a bunch of arcane-looking nonsense.
Still, after literally years of working with C++ as it is, to see someone use operator,() in this way kind of makes me go 'huh?' on the first mental parse. It's like some weird magical thing happened that doesn't seem immediately obvious, unless I know that the operator is being used.
I'd use it very judiciously, if at all, for that reason alone.
|
|
|
Goto Forum:
Current Time: Fri Sep 20 07:59:30 CEST 2024
Total time taken to generate the page: 0.03566 seconds
|