Std::min and Std::max Error resolution

Source: Internet
Author: User

C + + std::min and Std::max such as:

float_t f (float_t x) const {return Std::max (float_t) 0.0, x);}

However, the following code is used:

#include "windows.h"
#include <algorithm>
void foo () {
   int i = 5;
   int j = 7;
   int x = Std::max (i,j);
}

Compiling in VS can cause the following error:

1>test.cpp (7): Error C2589: ' (': illegal token on right side of ':: '

1>test.cpp (7): Error C2143:syntax Error : Missing '; ' Before ':: '

This is because the max and Min macros are also defined in Windows.h, and when you include Windows.h, the program cannot be compiled.

Solution:
1. Windows.h uses a different name, only for Windows systems.

int x = _cpp_max (i,j);
int y = _cpp_min (i,j);

This method changes on the system library and is not recommended.
2. Do not use the Std::max () and Std::min () functions in the system library and replace them with other alternatives such as:

int x = i > J? I:j; Max (i,j)
int y = i < J i:j;//min (i,j)

Simply re-implement these functions, and if you have large amounts of code, you can write a different name function yourself.
3. Add the namespace using namespace Std, or as follows:

Using Std::min;
Using Std::max;
int x = max (i,j);
int y = min (i,j);

This method experiment is not good, I do not know whether I understand wrong, welcome to discuss.
4. Use std::min<int> and std::max<int> as

int x = std::max<int> (i,j);
int y = std::min<int> (i,j);

This method requires prior knowledge of the input data type, but there are problems with different types I and J, such as:

int i = 5;
unsigned int j = 7;
int x = (Std::max) (i,j);
int y = (std::min) (I,J);

The following error occurs:

1>test.cpp (7): Error C2780: ' Const _ty &std::max (const _ty &,const _ty) ':
&,_PR 3 expects- 2 provided
1>        c:program filesmicrosoft Visual Studio 8vcincludexutility (3190): "*
declaration of ' std: : Max '
1>test.cpp (7): Error C2782: ' Const _ty &std::max (const _ty &,const _ty &) ':
template Parame Ter ' _ty ' is ambiguous
1>        c:program filesmicrosoft Visual Studio 8vcincludexutility (3182):
Declaration of ' Std::max '
1>        could be ' unsigned int '
1>        or ' int '

Summary: Recommended method 2, implement a function of the same function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.