General min and max - C++

前端 未结 2 1093
礼貌的吻别
礼貌的吻别 2021-01-12 02:25

Writing a general minimum function, Two questions came to my mind. The code works fine with any input type and different argument number:

namespace xyz
{

te         


        
2条回答
  •  半阙折子戏
    2021-01-12 02:29

    After the answer and worth comments I did it as below:

    template 
    auto min(const T1 &a, const T2 &b) 
    -> typename std::common_type::type
    {
        return a < b ? a : b;
    }
    
    template 
    auto min(const T1 &a, const T2 &b, const Args& ... args)
    -> typename std::common_type::type
    {
        return min(min(a, b), args...);
    }
    

提交回复
热议问题