Name Conflict between C ++ standard library functions and macro definitions

Source: Internet
Author: User

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?
 
 
  1. 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?
 
 
  1. #ifndef NOMINMAX
  2. #ifndef max
  3. #define max(a,b) (((a) > (b)) ? (a) : (b))
  4. #endif
  5. #ifndef min
  6. #define min(a,b) (((a) < (b)) ? (a) : (b))
  7. #endif
  8. #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?
 
 
  1. #if !__STDC__
  2. #ifndef _POSIX_
  3. /* Non-ANSI names for compatibility */
  4. #ifndef __cplusplus
  5. #define max(a,b) (((a) > (b)) ? (a) : (b))
  6. #define min(a,b) (((a) < (b)) ? (a) : (b))
  7. #endif
  8. ...
#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?
 
 
  1. (std::min)(x, y);
  2. (std::max)(x, y);
  3. (std::numeric_limits<T>::min)();
  4. (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?
 
 
  1. template <typename T, int Size>
  2. struct Series
  3. {
  4. T min() { return *(std::min_element(s, s + Size); }
  5. T& operator[](int index) { return s[index]; }
  6. private:
  7. T s[Size];
  8. };
  9. Series<int, 3> s;
  10. s[0] = 2;
  11. s[1] = 3;
  12. s[2] = 1;
  13. 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?
 
 
  1. 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?
 
 
  1. #ifndef NOMINMAX
  2. #ifndef max
  3. #define max(a,b) (((a) > (b)) ? (a) : (b))
  4. #endif
  5. #ifndef min
  6. #define min(a,b) (((a) < (b)) ? (a) : (b))
  7. #endif
  8. #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?
 
 
  1. #if !__STDC__
  2. #ifndef _POSIX_
  3. /* Non-ANSI names for compatibility */
  4. #ifndef __cplusplus
  5. #define max(a,b) (((a) > (b)) ? (a) : (b))
  6. #define min(a,b) (((a) < (b)) ? (a) : (b))
  7. #endif
  8. ...
#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?
 
 
  1. (std::min)(x, y);
  2. (std::max)(x, y);
  3. (std::numeric_limits<T>::min)();
  4. (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?
 
 
  1. template <typename T, int Size>
  2. struct Series
  3. {
  4. T min() { return *(std::min_element(s, s + Size); }
  5. T& operator[](int index) { return s[index]; }
  6. private:
  7. T s[Size];
  8. };
  9. Series<int, 3> s;
  10. s[0] = 2;
  11. s[1] = 3;
  12. s[2] = 1;
  13. 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.

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.