Avoiding const_cast when calling std::set::find

后端 未结 3 1700
心在旅途
心在旅途 2021-01-11 18:23

Is there any good way to obviate the const_cast below, while keeping const correctness?

Without const_cast the code below doesn\'t compile.

3条回答
  •  我在风中等你
    2021-01-11 19:24

    Yes.

    In C++14, you can use your own comparator that declares int const* as transparent. This would enable the template overload of find() that can compare keys against arbitrary types. See this related SO question. And here's Jonathan Wakely's explanation.

提交回复
热议问题