max函数或宏

    技术2022-05-12  5

    1、在g++中,max是个函数而不是宏,其定义在stl_algobase.h头文件中,代码为

     

     template<typename _Tp> inline const _Tp& max(const _Tp& __a, const _Tp& __b) { // concept requirements __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) //return __a < __b ? __b : __a; if (__a < __b) return __b; return __a; }

    注:引自 MinGW

     

    另外algorthim头文件会include  stl_algobase.h,因此在用的时候只直接include algorithm即可

     

    2、vc6.0中,没有该函数的定义,相似的微软定义了一个这样的函数_cpp_max,其代码为:

    template <class _Ty> inline const _Ty& _cpp_max(const _Ty& _X, const _Ty& _Y) {return (_X < _Y ? _Y : _X); }

    注:vs2005以后已有max的定义

    所以在vc6.0中max是不对的。

     

    3、在stdlib.h中还有一个这样的定义

     

    #define   __max(a,b)   (((a)   >   (b))   ?   (a)   :   (b))

     

    这里是一个宏,但这已经不是C++的,属于C的


    最新回复(0)