Should you overload swap in the std namespace?

前端 未结 5 2000
闹比i
闹比i 2020-12-06 00:22

I read something interesting today that said the \'standard\' way to call swap on a user provided type (provided as a template argument) is...

using std::swa         


        
5条回答
  •  醉话见心
    2020-12-06 01:04

    It is legal to provide specializations of standard templates for your own types, and those must go inside the std namespace. So both approaches are legal C++.

    That being said, the recommended way is to provide the swap free function in the same namespace as your own type and have ADL pickup your overload from there.


    EDIT: After some of the comments I reread the question and noticed that it mentions overloads in the std namespace. It is illegal to provide overloads in the std namespace, only specializations are allowed.

提交回复
热议问题