Why is the code in most STL implementations so convoluted?

后端 未结 6 1074
刺人心
刺人心 2021-01-30 06:15

The STL is a critical piece of the C++ world, most implementations derive from the initial efforts by Stepanov and Musser.

My question is given the criticality of the co

6条回答
  •  春和景丽
    2021-01-30 06:57

    Implementations vary. libc++ for example, is much easier on the eyes. There's still a bit of underscore noise though. As others have noted, the leading underscores are unfortunately required. Here's the same function in libc++:

    template 
    bool
    __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
    {
        _BidirectionalIterator __i = __last;
        if (__first == __last || __first == --__i)
            return false;
        while (true)
        {
            _BidirectionalIterator __ip1 = __i;
            if (__comp(*--__i, *__ip1))
            {
                _BidirectionalIterator __j = __last;
                while (!__comp(*__i, *--__j))
                    ;
                swap(*__i, *__j);
                _STD::reverse(__ip1, __last);
                return true;
            }
            if (__i == __first)
            {
                _STD::reverse(__first, __last);
                return false;
            }
        }
    }
    

提交回复
热议问题