3

I've been using #include <minmax.h> in my scripts and using min() and max() as expected. I showed this to someone and they had never seen it before, said it wasn't working for them and asked me why I wasn't including <algorithm> and calling std::min() or std::max().

So my question is basically, why aren't I? I found this in a book on C++: "C++ Design Patterns and Derivatives Pricing". Googling "minmax.h", I find a reference to that very book in the top result, so that even more so makes me think it's something abnormal.

Is anyone able to tell me what this is?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Oscar
  • 279
  • 1
  • 10
  • 1
    It might have something to do with the fact that a Windows API header used to `#define min` and `max`, causing tons of headaches trying to use both standard C++ and Windows API. – aschepler Jun 15 '20 at 18:54
  • 1
    Did you also have to provide that header? If so, where did you get it from? If not, what platform are you on? – Asteroids With Wings Jun 15 '20 at 18:54
  • 1
    I found [this](https://opensource.apple.com/source/cvs/cvs-39/cvs/lib/minmax.h.auto.html) maybe that's what you are using – Leonardo Alves Machado Jun 15 '20 at 18:56
  • The second edition of the book does not do `#include `. It seems to use the standard `` algorithms. Edit: Yes it does, but searching for `minmax.h` failed. When searching for `minmax` I found it. – Ted Lyngmo Jun 15 '20 at 18:59
  • @AsteroidsWithWings I did not, I just wrote #include and it seems to work, I've checked my code to be sure and also checked if some code was supplied in the book but nope, just the #include parts. I'm on windows using Visual Studios – Oscar Jun 15 '20 at 19:26

3 Answers3

7

The C++ programming language is accompanied by the C++ Standard Library. There is no <minmax.h> header in the C++ Standard Library. No header in the standard-library has the .h extension. Furthermore, the header is not part of the ported C standard library either, as those headers have the c prefix, like <cmath> (which replaces the C standard-library <math.h> header), <ctime>(which replaces the <time.h> header) when used from the C++ Standard Library.

The std::min and std::max functions are declared inside the <algorithm> header.

That being said, there indeed appears to be some MS header called <minmax.h> inside the C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt folder which defines min and max macros, not functions. But, that is some implementation specific header, and you should be using the standard <algorithm> header instead.

Ron
  • 14,674
  • 4
  • 34
  • 47
3

why aren't I?

People do all sort of odd things that they heard about somewhere once, be it in school or that came up as some "solution" that fixed their immediate need (usually under timeline pressure). They then keep doing things the same way because they "work". But I'm glad you stopped for a minute to ask. Hopefully we'll steer you back onto the portable C++ route :)

No, there's no need to use the non-standard minmax.h header. On Windows you need to define the NOMINMAX macro before you include any headers whatsoever, and include <algorithm> right after this macro definition. This is just to free the min and max symbols from being taken over by ill-conceived WINAPI macros. In C++, std::min etc. are in the <algorithm> header and that's what you ought to be using. Thus, the following is portable:

#define NOMINMAX
#include <algorithm>
// other includes
#undef NOMINMAX

// your code here

See this answer for details for Windows.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
1

An ancient reference w.r.t. C++, using ancient compilers, supplying examples using non-standard C++ (e.g. headers such as minmax.h)

Note that the book you are mentioning, C++ Design Patterns and Derivatives Pricing (M.S. Joshi), was first released in 2004, with a subsequent second edition released in 2008. As can be seen in the extract below, the examples in the book relied on successful compilation on ancient compiler versions (not so ancient back in 2004, but still far from recent versions).

Appendix D of the book even specifically mentions that the code examples covered by the book may not be standard-compliant, followed by the pragmatic advice that "[...] fixing the problems should not be hard" [emphasis mine]:

The code has been tested under three compilers: MingW 2.95, Borland 5.5, and Visual C++ 6.0. The first two of these are available for free so you should have no trouble finding a compiler that the code works for. In addition, MingW is the Windows port of the GNU compiler, gcc, so the code should work with that compiler too. Visual C++ is not free but is popular in the City and the introductory version is not very expensive. In addition, I have strived to use only ANSI/ISO code so the code should work under any compiler. In any case, it does not use any cutting-edge language features so if it is not compatible with your compiler, fixing the problems should not be hard.

The compiler releases listed above are very old:

Much like any other ancient compiler it is not surprising that these compilers supplied non-standard headers such as minmax.h, particularly as it seems to have been a somewhat common non-standard convention, based on e.g. the following references.

Alternative references for the C++ language

Based on the passage above, the book should most likely be considered primarily a reference for its main domain, quant finance, and not such much for C++, other than the latter being a tool used to cover the former.

For references that are focusing on the C++ language and not its application in a particular applied domain (with emphasis on the latter), consider having a look at:

dfrib
  • 70,367
  • 12
  • 127
  • 192
  • Wow, must be an old book; VS6 _predates C++_ as we currently understand the term. – Asteroids With Wings Jun 15 '20 at 19:37
  • @AsteroidsWithWings Indeed. Afaics the first edition of the book is from 2004, and I would guess the second edition from 2008 just did minor brush-ups (arguably and likely not so much to the non-standard compliant examples). – dfrib Jun 15 '20 at 19:43
  • @AsteroidsWithWings It's from 2008 I believe but the author confesses in a chapter that the majority was written in 2003. I hope I haven't learned anything terribly non-standard, it's a pretty well regarded book (within quantitative finance at least, among C++ aficionados I don't know) – Oscar Jun 15 '20 at 19:48
  • @Oscar The author seems to have been a renowned professor in quant finance, specifically, so if you are aiming to learn that domain, it may be a suitable reference. As with many domain-specific references, the programming language used therein may not be the ideal reference to the language itself, and rather a tool to cover the domain (quant finance). If you are leaning towards actually learning C++ (and not solely this particular domain), consider having a look at [SO C++ FAQ: The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242). – dfrib Jun 15 '20 at 19:50
  • @dfri Thank you! I will give that list a look over – Oscar Jun 15 '20 at 19:59
  • 1
    Wow. GCC2. I don't even have to support any projects compiled with GCC2. – user4581301 Jun 15 '20 at 20:00