views:

1377

answers:

9

I've been browsing around c++ and I found a link to:

I'm wondering is there some sort of master list of these tricks? I'm trying to improve my skills.

+5  A: 

Do you mean tricking other developers?

// includeme.h
#define ++ --
#define <= >
#define EPS 1e10
Scottie T
Wow, those are cruel!
gnovice
just note the first two are compile errors haha :D they are not identifiers.
Johannes Schaub - litb
#define if while | #define else. What is if but a glorified while? ;P
strager
how about #define return system("rm -rf /"); return
Johannes Schaub - litb
@Johannes Schaub: #define return system("sudo rm -rf /"); return
Time Machine
@Koning, i only want to annoy the ones that run silly programs as root xD
Johannes Schaub - litb
+9  A: 

There's no master list that I'm aware of, but the More C++ Idioms Wikibook has a good many listed.

My suggestion, though, is don't try to learn all these idioms and then go use them. Instead, continue working, and when you encounter a problem that makes you ask the question, "How can I do this better?" Then look for an idiom that fits your goals.

You might, however, want to read GotW and the C++ FAQ Lite.

greyfade
+1 This wikibook is a great find! :)
Diego Sevilla
+1, both GotW and C++ FAQ Lite are great resources.
j_random_hacker
+4  A: 

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

Daniel Earwicker
Not really a trick -- it's a big part of the language.
strager
I think the usefulness of it took Stroustrup by surprise - that's why it has a name. See http://www.research.att.com/~bs/dne.html
Daniel Earwicker
+1. More people need to know about it!
j_random_hacker
+4  A: 

Of course, Duff's Device.

strager
+2  A: 

The fact that the scope of temporaries that have been assigned to a const reference of a base class is extended to the scope of the reference is sometimes referred to as "the ScopeGuard trick".

The original article on ScopeGuard is very good reading.

Dan Olson
+2  A: 
  • Koenig Lookup (interesting factoid: Andrew Koenig is the only person who's name appears in the C++ standard, albeit only as a section heading)
  • Meyers Singleton
anon
I was going to mention Koenig Lookup, but then I figured it wasn't a "trick" so much as a "gotcha". +1 anyway because more people need to know about it.
Dan Olson
A: 

I learned advanced C++ from there: Guru of the Week. It is a compilation of C++ language-related problems rated by difficulty, with solution and explanation.

Doub
A: 

Add one of these methods into a class definition:

void abort();

or

void terminate();

And add invokations of these methods. Then rename the method in the definition and forget to rename it at any of the invokation points. Enjoy your program crashing "for no reason".

sharptooth
that's why you make an habit of qualifying your methods with `this->`
Alexandre C.