Rationale behind member function swap

天大地大妈咪最大 提交于 2019-12-02 10:17:44

The free function cannot see the private guts of the class, so the actual functionality needs to be provided as a class member. However, you want the free function because it's a customization point (using ADL); so a generic algorithm will call the free swap in order to achieve whatever swapping is appropriate for the type at hand.

The member function is also useful for constructions like T().swap(existing_thing), since the free function requires lvalue arguments.

I suppose the alternative would have been to make swap a public friend function declared inside the class, which could be found via ADL just as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!