Solved

std::min and std::max definitions

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
Unable to start eclipse ? 17 153
Error C2678: binary '!=': no operator found... 4 58
Finding Divisors 5 16
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

856 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