Solved

std::min and std::max definitions

Posted on 2007-03-26
5
5,253 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now