Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are your favorite C++ idioms? [duplicate]

Tags:

c++

idioms

Possible Duplicate:
What are your favorite C++ Coding Style idioms

Dear C++ fans,

Could you list here your favorite C++ idioms? It is better to list those which are useful yet not well-known.

Firstly, I put one of mine:

To avoid tedious repeating for (size_t i = 0; i < n; ++i) {...} , I use a macro like this:

#define LOOP(n) for (size_t _i = 0; _i < n; ++_i)

_i is a placeholder as bind uses.

So I can write:

vector<int> coll(100);
LOOP (100)
{
    coll[_i] = _i;
}

LOOP (100)
{
    auto a = _i;
    LOOP (100)
    {
        auto b = _i;
        cout << a << b;
    }
}
like image 246
xmllmx Avatar asked Nov 29 '22 05:11

xmllmx


2 Answers

RAII is on the top of my list. There are so many cases when it just comes in handy...

Most useful as a generic implementation like Andrei Alexandrescu's libloki.

like image 79
Marcus Borkenhagen Avatar answered Dec 15 '22 16:12

Marcus Borkenhagen


SFINAE

like image 21
Prasoon Saurav Avatar answered Dec 15 '22 14:12

Prasoon Saurav