Why is inequality tested as (!(a==b)) in a lot of C++ standard library code?

前端 未结 6 1795
忘了有多久
忘了有多久 2021-02-01 11:24

This is the code from the C++ standard library remove code. Why is inequality tested as if (!(*first == val)) instead of if (*first != val)

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 12:00

    Because this means the only requirement on T is to implement an operator==. You could require T to have an operator!= but the general idea here is that you should put as little burden on the user of the template as possible and other templates do need operator==.

提交回复
热议问题