Solved

std::min and std::max definitions

Posted on 2007-03-26
5
5,270 Views
Last Modified: 2012-06-21
I have the following statements in my stdafx.h file:

using std::min;
using std::max;

My compiler is telling me that min and max are not members of std.

What am I missing?

I am using VS 6.0 and have included the following:

atlbase.h
sql.h
sqlext.h
vector
list
cmath
time.h
algorithm
process.h
0
Comment
Question by:StanChart
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18793447
Try

#include <xutility>
using std::_cpp_min;
using std::_cpp_max;
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18793641
MSDN that came with VC6 says:

------------------------------------------------------------
min
template<class T>
    const T& min(const T& x, const T& y);
template<class T, class Pred>
    const T& min(const T& x, const T& y, Pred pr);
The first template function returns y if y < x. Otherwise it returns x. T need supply only a single-argument constructor and a destructor.

The second template function behaves the same, except that it replaces operator<(X, Y) with pr(X, Y).

Microsoft-Specific:
To avoid conflicts with min and max in WINDEF.H, use _MIN and _MAX instead. These macros evaluate to _cpp_min and _cpp_max, respectively.
END Microsoft-Specific
------------------------------------------------------------

So you either use jkr's solution or take _MIN and _MAX .

Regards, Alex
0
 

Author Comment

by:StanChart
ID: 18794209
I'm actually merging some code right now and what I previously had was:

using namespace std;

If I replace that with using std::_cpp_min/max, I get other errors like auto_ptr not defined, etc.
If I include both, I get "none of the 2 overloads have a best conversion".

The rest of the code base uses the std::min/max functions so I'd like to stick with those since it is not my code that i would be modifying.

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 18794274
And if you use

using namespace std;

it works?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 18794513
>>>> The rest of the code base uses the std::min/max functions
Unfortunately, VC6 was released prior to C++ Standard, so its STL implementation is not compliant in many points.  

In the MSDN comment they told that there is a conflict with with (macro) definitions in WINDEF.H. You can get rid of that macro by adding NOMINMAX to the preprocessor macros of your project settings. Then in your cpp files you could add at the top (below inlcude of stdafx.h if using PCH).

#ifndef min
#define min_defined
#define min std::_cpp_min
#endif

#ifndef max
#define max_defined
#define max std::_cpp_max
#endif

and et the bottom of the cpp:

#ifdef min_defined
#undef min
#endif
#ifdef max_defined
#undef max
#endif

Of course you could put both sequences into a header file which can be included then.

Isn't it ugly? Maybe a better way is to copy the template definition of _ccp_min and _cpp_max, put them to a new header file and rename the function names to min and max accordingly. Put both function to the namespace std. If you uccessfully suppressed any other occurrence fo min and max you can include that header what should make valid the above usage clauses.

Regards, Alex




0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question