Can I selectively (force) inline a function?

后端 未结 6 565
执念已碎
执念已碎 2020-12-28 13:46

In the book Clean Code (and a couple of others I have come across and read) it is suggested to keep the functions small and break them up if they become large. It also sugge

6条回答
  •  醉酒成梦
    2020-12-28 14:22

    This is as much about good old fashioned straight C as it is about C++. I was pondering this the other day, because in an embedded world, where both speed and space need to be carefully managed, this can really matter (as opposed to the all too oft "don't worry about it, your compiler is smart and memory is cheap prevalent in desktop/server development).

    A possible solution that I have yet to vet is to basically use two names for the different variants, something like

    inline int _max(int a, int b) {
        return a > b ? a : b;
    }
    

    and then

    int max(int a, int b) {
        return _max(a, b);
    }
    

    This would give one the ability to selectively call either _max() or max() and yet still having the algorithm defined once-and-only-once.

提交回复
热议问题