Implementing variadic min / max functions

后端 未结 6 1940
灰色年华
灰色年华 2020-12-14 06:06

I\'m implementing variadic min/max functions. A goal is to take advantage of the compile time known number of arguments and perform an unrolled evaluation (

6条回答
  •  天命终不由人
    2020-12-14 06:54

    I appreciate the thought Yakk put into return types so I wouldn't have to, but it gets a lot simpler:

    template
    T&& vmin(T&& val)
    {
        return std::forward(val);
    }
    
    template
    auto vmin(T0&& val1, T1&& val2, Ts&&... vs)
    {
        return (val1 < val2) ?
          vmin(val1, std::forward(vs)...) :
          vmin(val2, std::forward(vs)...);
    }
    

    Return type deduction is pretty awesome (may require C++14).

提交回复
热议问题