Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

std::min and std::max definitions

Posted on 2007-03-26
5
Medium Priority
?
5,338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

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