Jump to content

  • Log In with Google      Sign In   
  • Create Account


Favorite little known or underused C++ features


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
46 replies to this topic

#21 Servant of the Lord   Crossbones+   -  Reputation: 18203

Like
1Likes
Like

Posted 23 October 2012 - 02:31 PM


anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.


Crap, I've been looking the standard up and down and I didn't find anything that would make this illegal. Though I could've looked harder. Maybe there's a difference between C11 and C++11 in this regard? But I do know that it's not just a visual studio extension; it works well on gcc and clang as well.

GCC does it just because Visual Studio does it, and people would complain if GCC was too incompatible with Microsoft's compiler.

If you have compiler warnings set to their proper level, GCC would tell you that it's non-standard, iirc (I ran into this a few months back). Though, the documentation for GCC does imply that C11 permits it.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


Sponsor:

#22 joew   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 23 October 2012 - 03:14 PM

I should note that the only places I've actually seen people use anonymous unions/structures was usually almost always a hack that could have been avoided.

I used to use it awhile back in a matrix class but killed it off when fixing standards issues in the codebase.

[source lang="cpp"]struct Matrix44{ union { struct { float _m[16]; }; struct { float m[4][4]; }; };};[/source]

Edited by joew, 23 October 2012 - 03:15 PM.


#23 swiftcoder   Senior Moderators   -  Reputation: 9762

Like
1Likes
Like

Posted 23 October 2012 - 03:26 PM


I should note that the only places I've actually seen people use anonymous unions/structures was usually almost always a hack that could have been avoided.

I used to use it awhile back in a matrix class but killed it off when fixing standards issues in the codebase.

If you really need to do something like this, then an array decays to a pointer. You can always store just the 2-dimensional array and cast back to the single dimension as needed.

Though I would submit that you shouldn't be handling the data in two separate fashions here, unless you are providing some sort of abstraction over both.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#24 ChaosEngine   Crossbones+   -  Reputation: 2247

Like
0Likes
Like

Posted 23 October 2012 - 03:29 PM

variable arguments
...
^ is awesome


No, it's bloody not.
"Basically whenever you invoke the dread ellipses construct you leave the happy world of type safety." — SiCrane
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#25 swiftcoder   Senior Moderators   -  Reputation: 9762

Like
2Likes
Like

Posted 23 October 2012 - 03:54 PM


variable arguments
...
^ is awesome


No, it's bloody not.
"Basically whenever you invoke the dread ellipses construct you leave the happy world of type safety." — SiCrane


Thus why we now need variable template arguments, to fix the deficiencies previously solved via variadic arguments :)

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#26 joew   Crossbones+   -  Reputation: 3634

Like
2Likes
Like

Posted 23 October 2012 - 03:58 PM

If you really need to do something like this, then an array decays to a pointer. You can always store just the 2-dimensional array and cast back to the single dimension as needed.

I have no idea why it was like that as the code was extremely old and just never touched. I believe it was due to passing to an API but that still doesn't make any sense considering the cast is a NOP so it wouldn't even save anything.

Edited by joew, 23 October 2012 - 03:58 PM.


#27 Radikalizm   Crossbones+   -  Reputation: 2801

Like
0Likes
Like

Posted 23 October 2012 - 04:00 PM

Thus why we now need variable template arguments, to fix the deficiencies previously solved via variadic arguments


I was so looking forward to this feature, only to find out that visual studio 2012 would not be supporting it. I really hope microsoft implements at least the really important features of C++11 soon, they're seriously running behind gcc right now.

Edited by Radikalizm, 23 October 2012 - 04:02 PM.

I gets all your texture budgets!


#28 Washu   Senior Moderators   -  Reputation: 4688

Like
1Likes
Like

Posted 23 October 2012 - 04:03 PM


If you really need to do something like this, then an array decays to a pointer. You can always store just the 2-dimensional array and cast back to the single dimension as needed.

I have no idea why it was like that as the code was extremely old and just never touched. I believe it was due to passing to an API but that still doesn't make any sense considering the cast is a NOP so it wouldn't even save anything.

The most common cases I've seen that in is crappy game tutorials code.

Also, I like your nickname. It's much better...now that I've changed it.

Edited by Washu, 23 October 2012 - 04:04 PM.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#29 joew   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 23 October 2012 - 04:16 PM

Also, I like your nickname. It's much better...now that I've changed it.

rofl bah fix it!

#30 jwezorek   Crossbones+   -  Reputation: 1772

Like
0Likes
Like

Posted 23 October 2012 - 04:25 PM


Also, I like your nickname. It's much better...now that I've changed it.

rofl bah fix it!


Personally I liked "joew" seeing as it is the *ultimate* first name plus last initial.

#31 Washu   Senior Moderators   -  Reputation: 4688

Like
0Likes
Like

Posted 23 October 2012 - 04:26 PM



Also, I like your nickname. It's much better...now that I've changed it.

rofl bah fix it!


Personally I liked "joew" seeing as it is the *ultimate* first name plus last initial.

Well, I was going to go for [Joe the:Objective c:Lover] but it wouldn't accept that.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#32 joew   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 23 October 2012 - 04:32 PM

Don't make me whine to Drew! Posted Image

#33 Washu   Senior Moderators   -  Reputation: 4688

Like
0Likes
Like

Posted 23 October 2012 - 04:34 PM

Don't make me whine to Drew! Posted Image

Your wish has been granted. :D

Edited by Washu, 23 October 2012 - 04:35 PM.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#34 joew   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 23 October 2012 - 04:35 PM

Your wish has been granted.

To joew!

EDIT: not funny!

Edited by joew aka The Woynillowicz, 23 October 2012 - 04:39 PM.


#35 Washu   Senior Moderators   -  Reputation: 4688

Like
0Likes
Like

Posted 23 October 2012 - 04:39 PM


Your wish has been granted.

To joew!


I'm CONFUSED?!?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#36 joew   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 23 October 2012 - 04:43 PM

I'm CONFUSED?!?

So am I.... this change actually introduces a compile error for some wtf reason:

[source lang="cpp"]// OriginalRectangle& operator=(const Rectangle& rhs) : left(rhs.left), bottom(rhs.bottom), right(rhs.right), top(rhs.top) {}// Replaced withRectangle& operator=(const Rectangle&) = default;[/source]

EDIT: and yeah it's just a pod struct

Edited by joew, 23 October 2012 - 04:43 PM.


#37 Washu   Senior Moderators   -  Reputation: 4688

Like
0Likes
Like

Posted 23 October 2012 - 04:44 PM


I'm CONFUSED?!?

So am I.... this change actually introduces a compile error for some wtf reason:

[source lang="cpp"]// OriginalRectangle& operator=(const Rectangle& rhs) : left(rhs.left), bottom(rhs.bottom), right(rhs.right), top(rhs.top) {}// Replaced withRectangle& operator=(const Rectangle&) = default;[/source]

EDIT: and yeah it's just a pod struct


If you're going to do one assignment operator, you should do both (move and copy).

As an additional note: VS2012 does not support default nor deleted

Edited by Washu, 23 October 2012 - 04:47 PM.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#38 joew   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 23 October 2012 - 04:45 PM

If you're going to do one assignment operator, you should do both (move and copy).

I do :)

#39 joew   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 23 October 2012 - 04:49 PM

As an additional note: VS2012 does not support default nor deleted

Gross! I'll stick with clang which has the majority of the standard in Posted Image

EDIT: FYI the issue was just that another class using Rectangle didn't have operator= defined ... that's nice it makes it easy to track down during a cleaning day like today

Edited by joew, 23 October 2012 - 04:52 PM.


#40 mhagain   Crossbones+   -  Reputation: 7610

Like
2Likes
Like

Posted 23 October 2012 - 07:03 PM

I once used negative array indices in combination with a semi-Duff's Device - and winced in mixed horror/pride afterwards. But it was in the days when C-abuse like that used to please me (I've sinced learned).

Statements like this make me smile:
if ((time = (newtime = GetTime ()) - oldtime) > 0)

Wouldn't do it in real code, but it gets a "hey! you can do this!" comment in stuff I'm fooling around with.

Edited by mhagain, 23 October 2012 - 07:04 PM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS