Resolving name pollution caused by macro definitions in C + +

Source: Internet
Author: User

When writing a Graph template class, in order to enable the user to customize the type of distance, such as int or double or even other high-precision rational number encapsulation class,
I defined the type of distance as template parameter t_dist and used library functions in the standard library

STD :: numeric_limits < t_dist : : Max ()

To define the maximum distance

Template<TypeName t_dist>ConstTypeNameTopologicalgraph<t_dist: :DistanceTopologicalgraph<t_dist: :max_distance=    (STD::numeric_limits<t_dist: :Max()/static_cast<t_dist> (2));

In a unit test, a Windows.h is included in the trace class that tracks and records the execution state when the program crashes.
And I'm using the include model to implement the template, and in the test project Trace.h before GRAPH.H,
Causes Std::numeric_limits<t_dist>::max () to conflict with the MAX () macro in Windows.h, so the macro substitution done in the preprocessing phase causes the statement to have an inexplicable syntax error.

Later on, StackOverflow looked up a bit,
Useful #undef Max, there are compiler switch macros that use other Windows, have Windows.h in the CPP file, and say don't include Windows.h ...

Finally found a beautiful solution--
Http://stackoverflow.com/questions/1394132/macro-and-member-function-conflict

In fact, just use parentheses to enclose Max ...

(std::numeric_limits<t_dist;::Max )()


So, still need to add parentheses AH ~
Previously only know the definition of macros to be wrapped up layer by bracket, did not expect to use the macro or syntax structure and macro-similar syntax elements (such as function calls) this time!


..................

After solving this problem, it suddenly appears that the package class cannot use Std::numeric_limits<t_dist>::max () to get the correct maximum value ...

Resolving name pollution caused by macro definitions in C + +

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.