6

I want to use #define NOMINMAX in my Visual Studio C++ project using MFC, so that I can use std::min and std::max. However, when I put this line in my stdafx.h, I get following compile error:

c:\program files (x86)\windows kits\8.0\include\um\GdiplusTypes.h(475): error C3861: 'min': identifier not found

I am not using GDI+ intentionally, this is something MFC must be doing. Can the issue be fixed somehow, either by removing the GDI+, or by adjusting it to compile?

Suma
  • 33,181
  • 16
  • 123
  • 191
  • There seems to be a similar question with an answer mentioning a workaround, however I do not understand the steps describing the workaround: http://stackoverflow.com/a/4914108/16673 – Suma Apr 09 '13 at 11:16

2 Answers2

11

I don't work on Windows so I'm not used to dealing with this, and I'm not testing this, but I believe that answer is suggesting you do this:

#define NOMINMAX
#include <algorithm>
namespace Gdiplus
{
  using std::min;
  using std::max;
};
//... your other includes.

This will get the "proper" versions of min and max, and make them available without the std:: prefix (which seems to be how it is used in the GdiplusTypes.h header).

Suma
  • 33,181
  • 16
  • 123
  • 191
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • This removes the compile error, however it brings min and max into the to global scope, which the answer specifically mentions as a bad thing. However, it helped me to make the sense of it. I have edited the code to achieve what was described in that answer. – Suma Apr 09 '13 at 11:37
  • 1
    @Suma See my answer for a solution that _doesn't_ bring min and max into the global scope. – James M Apr 10 '13 at 16:59
5

The winapi min and max are macros, so you can just #undef them after including the MFC or winapi headers:

#undef min
#undef max
James M
  • 18,506
  • 3
  • 48
  • 56