views:

1158

answers:

7

Question

What C++ idioms should C++ programmers know?

By C++ idioms, I mean design patterns or way of doing certain things that are only applicable for C++ or more applicable for C++ than most other languages.


Edit:

Please explain why one should use the idioms and what the idioms do.

+6  A: 

PIMPL, aka P ointer to IMPL ementation ?

Dmitry
I personally hate it. :/
GMan
Well, you don't have to use it everywhere :)
Dmitry
Technically, you don't _have_ to use it _anywhere_. :-)
James McNellis
Unfortunately, binary compatibility is important sometimes, and it's the only practical way to ensure it.
Pavel Minaev
It also significantly reduces compilation time (by reducing explicit dependencies), and god knows how C++ programmers suffer from it!
Matthieu M.
On the other hand, without long compile times, many of us would not be able to spend _nearly_ as much time here on StackOverflow.
James McNellis
+29  A: 

By far the single most important "pattern" to learn and know that's (nearly) unique to C++ is RAII (Resource Acquisition Is Initialization).

Edit: (To answer extra question edited into the question). You use RAII primarily to (semi-)automate resource management. The most obvious use is freeing resources owned by objects when the owning objects go out of scope, such as freeing memory or closing files.

Jerry Coffin
+1. Unfortunately also the idiom with the worst name.
the_mandrill
Don;t get me wrong its one of the more important ones in C++, but its not even close to being unique to C++. Though C++ has made it famous.
Martin York
I think it's not about things that are unique to C++; rather, it's about things that are uniquely ubiquitous in C++; something you can't really get by without knowing, which you possibly could in some other languages. RAII would definitely be such a thing.
Pavel Minaev
Agree, RAII is super important in C++, being smart pointers an integral part of it.
Seth Illgard
The question clearly states: "things that are only applicable for C++ "
Martin York
@Martin: "only applicable for C++ or more applicable for C++ than most other languages."
Jerry Coffin
RAII is the C++ way of implementing the /release pattern/.
Luc Hermitte
C++ has made it famous because memory leaks do not appear in GC-language and memory is by far the most obvious resource. Unfortunately it means that people that are unaware of this idiom have trouble managing DB Connections or sockets etc... the 'using' construct is retarded in my opinion since it puts the burden on EVERY user instead of the sole class writer :/
Matthieu M.
+4  A: 

Template metaprogramming. It's great because it's basically compile-time duck typing, so you get most of the flexibility of duck typing with the speed of static typing.

dsimcha
isnt it duck taping?
Eric
Ducks *can't* be taped.
greyfade
+5  A: 

If you want to get the most out of the STL then iterators and functors/function objects are essential idioms. The use of iterators also implicitly relies on the 'half-open range' idiom too.

the_mandrill
Very true! Have you heard about sequence constructors and conversion constructors?
Partial
Do you mean like `iota()`? Another omission from the list is generator objects.
the_mandrill
To be honest, I am not familiar with iota(), but I've posted an answer with an example.
Partial
+14  A: 

Here is one list. If I had to pick a couple I might go with the Curiously Recurring Template Pattern or Virtual Contstructors.

Duck
+1 I like the list.
Martin York
+1 For uber list :P
Partial
Great list I often consult; however Curiously recurring Template would be very low on my list as something every C++ programmer would need to know.
Elemental
@Elemental - Agreed. I may have misread the question but I thought it mentioned uniquely C++ and CRTP seems to qualify in that respect.
Duck
+3  A: 

RAII, COW, pimpl, law of demeter (not sure if can be classified as idiom), type traits and policies. (COW and law of demeter are not limited to C++ though)

BostonLogan
Traits is somthing I have not seen anywhere else (but then again I have not been looking that hard).
Martin York
+2  A: 

I prohibit default copy constructors and assignment operators. I actually go beyond that, but those are the most common. I think life would be easier if they were not implicit.

Justin