-2

There is simple max() function code in algorithm file. Maybe it's c++ code but i understand nothing here. There are a lot of macros. Can you explain me how to understand this type of code? And why are there a lot of #define used ?

        // TEMPLATE FUNCTION max
template<class _Ty> inline

    _Post_equal_to_(_Left < _Right ? _Right : _Left)

    constexpr const _Ty& (max)(const _Ty& _Left, const _Ty& _Right)
        _NOEXCEPT_OP(_NOEXCEPT_OP(_DEBUG_LT(_Left, _Right)))
    {   // return larger of _Left and _Right
    return (_DEBUG_LT(_Left, _Right) ? _Right : _Left);
    }

template<class _Ty> inline
    /* constexpr */ // TRANSITION
    _Ty (max)(_XSTD initializer_list<_Ty> _Ilist)
    {   // return leftmost/largest
    return ((_STD max)(_Ilist, less<>()));
    }

1 Answers1

0

"HOW to understand" complex code, is simply walking through it, one word (or line) at a time, and understanding exactly what it means. Often the best way to do this when you're starting out is to just find any #defines and copy paste them in place.

Taking the first example (note this is HOW to work it out, not what it does)

template<class _Ty> inline                       

Ok, so we're looking at an inline template... I can look up on cppreference what they mean

_Post_equal_to_(_Left < _Right ? _Right : _Left)

Looks like a macro (because it's outside any {}) ... best find what that definition is, and post it in its place. If it calls other macros (it does) then I'll have to repeat until I understand exactly what it's doing.

constexpr const _Ty& (max)(const _Ty& _Left, const _Ty& _Right)

constexpr & const ... those I can look up at cpprefernce too. _Ty& well, I know from my reading of template at cppreference that this is returning the type that the template is based on; and we get a return value and a left&right.

Maybe I've not seen that syntax (max) before for declaring a function... And I can't find any documentation on what's going on; and I'm not able to understand / find the c++ standard. I'll ask on Stack Overflow if it's valid syntax and if there's any reason to pick it over just max() as that's a specific problem which I can post the results of research about; but I know it's accepted in MY compiler because I've tried it.

    _NOEXCEPT_OP(_NOEXCEPT_OP(_DEBUG_LT(_Left, _Right)))

More macros - well, best find and paste those; it's actually related to another question Use of the noexcept specifier in function declaration and definition? So I'm going to read that to fast forward and save me time.

{   // return larger of _Left and _Right
return (_DEBUG_LT(_Left, _Right) ? _Right : _Left);
}

Well, only one macro to go (because it's all capitals) but we've already seen this one before.. and the helpful comment says what it does.

To answer the latter - why so many #define - that's quite simple. They wanted to. They didn't need to, and some would tend to frown upon their continued use. I don't think it's worth expanding on that point further since it's not going to help anyone.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • I just don't understand why those 15 lines of code with lot of macros is better than this ? `template inline _Ty my_max(_Ty x, _Ty y) { return x > y ? x : y; }` – Mate Gvenetadze Feb 09 '18 at 14:05
  • 1. not 15; just 7 for the 1st function. 2. did you do all the steps, because `constexpr` may have a big impact on performance here... you're still trying to understand why it's different, while not understanding what it all means – UKMonkey Feb 09 '18 at 14:11
  • `template inline constexpr const _Ty& my_max(const _Ty& x,const _Ty& y) { return x > y ? x : y; }` How about now ? Yes maybe i don't know everything but i know syntax very well. So i'm trying to understand point of macros. – Mate Gvenetadze Feb 09 '18 at 14:16
  • Macros can change depending on other defines. so DEBUG_LT likely does something different between debug and release builds. How do they differ? Well, I'm sure you'll work it out once you've found their definitions and expanded them. – UKMonkey Feb 09 '18 at 14:20
  • `#define _DEBUG_LT(x, y) \ _DEBUG_LT_IMPL(x, y, _FILENAME, __LINE__)` and `#define _DEBUG_LT_IMPL _Debug_lt` Do you mean this definition? Where can i find explanation of this ? – Mate Gvenetadze Feb 09 '18 at 14:22
  • just keep expanding. – UKMonkey Feb 09 '18 at 14:24
  • "find any #defines and copy paste them in place" - a good IDE will expand for you, if you wish. For instance, Eclipse CDT will, if you just hover the cursor. – Mawg says reinstate Monica Feb 09 '18 at 15:14
  • And, with the GCC compiler, "If you use the '-E' option, nothing is done except preprocessing". So, you can run your source through the compiler and it will generate source with all macros expanded. No need to do it manually. – Mawg says reinstate Monica Feb 09 '18 at 15:15
  • @Mawg They likely only do the first expansion; but if it's a macro wrapping a macro, wrapping a function (like Debug_LT) then it doesn't help *enough* – UKMonkey Feb 09 '18 at 15:16
  • "likely"? Are you sure? I would need to check that & get mack to you. There is always gcc -E, which I commented at the same time as you – Mawg says reinstate Monica Feb 09 '18 at 15:17
  • @Mawg VS also offers the ability to do the same; but if you're trying to understand a single function in a large project, it can end up being like trying to break a nut with a sledge hammer – UKMonkey Feb 09 '18 at 15:18