Search

Allow type safe alternatives for the min() and max() macros to be used in MFC projects. by Frank Heimes

Closed
as Fixed Help for as Fixed

4
0
Sign in
to vote
Type: Suggestion
ID: 584688
Opened: 8/9/2010 3:12:17 AM
Access Restriction: Public
0
Workaround(s)
Please allow MFC headers to compile after defining type safe alternatives for the min()- and max() macros.

In the past, we had to fix subtle bugs in our code caused by inappropriate use of the min() and max() macros. These cause an automatic type conversion by the compiler, which may not lead to the desired results (e.g. when comparing signed/unsigned values or values of different size). Even worse, one of the argument to min() or max() may be evaluated twice, inadvertently.

Therefore we added code to the beginning of every project for mapping the default min() and max() macros to the definitions in std::min() and std::max(). This forces all calls to min() and max() to be type safe; i.e. the programmer must decide which types the compared values should have and perform an explicit type cast.

These type safe definitions to not compile for (very) few source code locations.
Details (expand)

Product Language

English

Visual Studio/Silverlight/Tooling version

Visual Studio 2010

Operating System

Windows 7

Operating System Language

English

Steps to reproduce

1) Start VS2010 on Vista or Win7
2) Create new project: MFC Application
3) Click "Finish", i.e. use wizzard default settings.
4) Build Solution
5) Run target
> Application starts up and works as expected.

6) Insert the following lines at the beginning of stdafx.h
#define NOMINMAX
#define _INC_MINMAX
#include <xutility>
using std::min;
using std::max;

7) Build Solution
The following compiler errors occur:
Error    1    AllowTypeSafeMinMax    error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtoolbar.h    171
Error    2    AllowTypeSafeMinMax    error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtoolbar.h    171
Error    3    AllowTypeSafeMinMax    error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxmenubar.h    161
Error    4    AllowTypeSafeMinMax    error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxmenubar.h    161
Error    5    AllowTypeSafeMinMax    error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxdesktopalertwnd.h    81
Error    6    AllowTypeSafeMinMax    error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxdesktopalertwnd.h    81

See the attached sample project "AllowTypeSafeMinMax.zip"

Although these error messages are somewhat cryptic and C2780 is actually misleading, the remedy to fix this is simply an explicit type cast at the following code lines:

afxtoolbar.h(171)
afxmenubar.h(161)
afxdesktopalertwnd.h(81)

Please see the modified versions of these files in the folder "AllowTypeSafeMinMax\Fixed" of the sample project. Only the aforementioned headers are modified. The other headers have been included unmodified so that the project can be compiled without additional customizations.

You may also want to correct one line in the sample code of the C++/MFC Project Wizzard template files: OutputWnd.cpp(100)

Actual results

The following compiler errors occur:
Error    1    AllowTypeSafeMinMax    error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtoolbar.h    171
Error    2    AllowTypeSafeMinMax    error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtoolbar.h    171
Error    3    AllowTypeSafeMinMax    error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxmenubar.h    161
Error    4    AllowTypeSafeMinMax    error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxmenubar.h    161
Error    5    AllowTypeSafeMinMax    error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxdesktopalertwnd.h    81
Error    6    AllowTypeSafeMinMax    error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided    c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxdesktopalertwnd.h    81

Expected results

No compiler errors
File Attachments
File Name Submitted By Submitted On File Size  
AllowTypeSafeMinMax.zip 8/9/2010 777 KB
Sign in to post a comment.
Posted by Microsoft on 8/20/2010 at 2:24 PM
Hello,

Thanks for the report. This issue has been fixed in MFC for the next major release of Visual Studio.

Pat Brenner
Visual C++ Libraries Development
Posted by Microsoft on 8/9/2010 at 8:34 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.