A min= idiom in C++?

后端 未结 7 935
旧巷少年郎
旧巷少年郎 2021-02-09 22:10

We use

x += y

instead of

x = x + y

And similarly for *,/,- and other operators. Well, what about

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-09 22:44

    You can't extend the language in this way. The closest you can come is something like:

    template 
    T&
    mineq( T& lhs, U rhs )
    {
        if ( rhs < lhs ) {
            lhs = rhs;
        }
        return lhs;
    }
    

    This would allow writing:

    mineq( x, y );
    

    I question whether it's worth the bother, however.

提交回复
热议问题