To http://blog.csdn.net/rnamatrix/article/details/5765462
Today, when using the numeric_limits class template functions Max and min in limits, an error occurs during compilation:
[c-sharp] view plaincopyprint?
- error C2589: '(' : illegal token on right side of '::'
error C2589: '(' : illegal token on right side of '::'
I was puzzled. Then I suddenly thought that Max and Min may have been defined as macros. So I checked the header file and found that it must be in <windows. h>:
Windows. h Includes the windef. h header file, which defines Macros in windef. h:
[c-sharp] view plaincopyprint?
- #ifndef NOMINMAX
-
- #ifndef max
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #endif
-
- #ifndef min
- #define min(a,b) (((a) < (b)) ? (a) : (b))
- #endif
-
- #endif /* NOMINMAX */
#ifndef NOMINMAX#ifndef max#define max(a,b) (((a) > (b)) ? (a) : (b))#endif#ifndef min#define min(a,b) (((a) < (b)) ? (a) : (b))#endif#endif /* NOMINMAX */
Therefore, the max and Min macro definitions here conflict with the numeric_limits <*>: Max/min definitions in the standard template library, and the # define nominmax problem is solved.
(Note that the macro scope is global)
Go to Google and find out which common header files the similar max/min macros are defined in.
It is pointed out that the macro definition of max/min also appears in the stdlib. h and MINMAX. h header files.
In stdlib. h:
[c-sharp] view plaincopyprint?
- #if !__STDC__
-
- #ifndef _POSIX_
-
- /* Non-ANSI names for compatibility */
-
- #ifndef __cplusplus
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #define min(a,b) (((a) < (b)) ? (a) : (b))
- #endif
-
- ...
#if !__STDC__#ifndef _POSIX_/* Non-ANSI names for compatibility */#ifndef __cplusplus#define max(a,b) (((a) > (b)) ? (a) : (b))#define min(a,b) (((a) < (b)) ? (a) : (b))#endif...
Generally, _ max and _ min macros are used in ansi c. Max and min are defined only in non-standard C. Therefore, stdlib is included in standard C. the H header file does not conflict with the max/min macro.
The MINMAX. h header file is generally included only when you want to use the max/min macro.
In addition, another solution to this problem is found on the Internet:
[c-sharp] view plaincopyprint?
- (std::min)(x, y);
- (std::max)(x, y);
- (std::numeric_limits<T>::min)();
- (std::numeric_limits<T>::max)();
(std::min)(x, y);(std::max)(x, y);(std::numeric_limits<T>::min)();(std::numeric_limits<T>::max)();
In this way, the function name is enclosed in parentheses, and Max/min is no longer replaced as a macro with parameters, so conflicts can be avoided.
This solution can be used when a member function of a user-defined type conflicts with a global macro definition.
[c-sharp] view plaincopyprint?
- template <typename T, int Size>
- struct Series
- {
- T min() { return *(std::min_element(s, s + Size); }
- T& operator[](int index) { return s[index]; }
- private:
- T s[Size];
- };
-
- Series<int, 3> s;
- s[0] = 2;
- s[1] = 3;
- s[2] = 1;
-
- int m = (s.min)(); // long way, but here is the trick
template <typename T, int Size>struct Series{ T min() { return *(std::min_element(s, s + Size); } T& operator[](int index) { return s[index]; }private: T s[Size];};Series<int, 3> s;s[0] = 2;s[1] = 3;s[2] = 1;int m = (s.min)(); // long way, but here is the trick
However, this method may have some negative effects:
In this way, ADL (argument depended Name Lookup) cannot be used. Therefore, the class name or namespace domain name must be added before the member function, that is, fully qualified.
To http://blog.csdn.net/rnamatrix/article/details/5765462
Today, when using the numeric_limits class template functions Max and min in limits, an error occurs during compilation:
[c-sharp] view plaincopyprint?
- error C2589: '(' : illegal token on right side of '::'
error C2589: '(' : illegal token on right side of '::'
I was puzzled. Then I suddenly thought that Max and Min may have been defined as macros. So I checked the header file and found that it must be in <windows. h>:
Windows. h Includes the windef. h header file, which defines Macros in windef. h:
[c-sharp] view plaincopyprint?
- #ifndef NOMINMAX
-
- #ifndef max
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #endif
-
- #ifndef min
- #define min(a,b) (((a) < (b)) ? (a) : (b))
- #endif
-
- #endif /* NOMINMAX */
#ifndef NOMINMAX#ifndef max#define max(a,b) (((a) > (b)) ? (a) : (b))#endif#ifndef min#define min(a,b) (((a) < (b)) ? (a) : (b))#endif#endif /* NOMINMAX */
Therefore, the max and Min macro definitions here conflict with the numeric_limits <*>: Max/min definitions in the standard template library, and the # define nominmax problem is solved.
(Note that the macro scope is global)
Go to Google and find out which common header files the similar max/min macros are defined in.
It is pointed out that the macro definition of max/min also appears in the stdlib. h and MINMAX. h header files.
In stdlib. h:
[c-sharp] view plaincopyprint?
- #if !__STDC__
-
- #ifndef _POSIX_
-
- /* Non-ANSI names for compatibility */
-
- #ifndef __cplusplus
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #define min(a,b) (((a) < (b)) ? (a) : (b))
- #endif
-
- ...
#if !__STDC__#ifndef _POSIX_/* Non-ANSI names for compatibility */#ifndef __cplusplus#define max(a,b) (((a) > (b)) ? (a) : (b))#define min(a,b) (((a) < (b)) ? (a) : (b))#endif...
Generally, _ max and _ min macros are used in ansi c. Max and min are defined only in non-standard C. Therefore, stdlib is included in standard C. the H header file does not conflict with the max/min macro.
The MINMAX. h header file is generally included only when you want to use the max/min macro.
In addition, another solution to this problem is found on the Internet:
[c-sharp] view plaincopyprint?
- (std::min)(x, y);
- (std::max)(x, y);
- (std::numeric_limits<T>::min)();
- (std::numeric_limits<T>::max)();
(std::min)(x, y);(std::max)(x, y);(std::numeric_limits<T>::min)();(std::numeric_limits<T>::max)();
In this way, the function name is enclosed in parentheses, and Max/min is no longer replaced as a macro with parameters, so conflicts can be avoided.
This solution can be used when a member function of a user-defined type conflicts with a global macro definition.
[c-sharp] view plaincopyprint?
- template <typename T, int Size>
- struct Series
- {
- T min() { return *(std::min_element(s, s + Size); }
- T& operator[](int index) { return s[index]; }
- private:
- T s[Size];
- };
-
- Series<int, 3> s;
- s[0] = 2;
- s[1] = 3;
- s[2] = 1;
-
- int m = (s.min)(); // long way, but here is the trick
template <typename T, int Size>struct Series{ T min() { return *(std::min_element(s, s + Size); } T& operator[](int index) { return s[index]; }private: T s[Size];};Series<int, 3> s;s[0] = 2;s[1] = 3;s[2] = 1;int m = (s.min)(); // long way, but here is the trick
However, this method may have some negative effects:
In this way, ADL (argument depended Name Lookup) cannot be used. Therefore, the class name or namespace domain name must be added before the member function, that is, fully qualified.