Invoking begin and end via using-directive?

后端 未结 4 2118
刺人心
刺人心 2021-02-19 01:44

The established idiom for invoking swap is:

using std::swap
swap(foo, bar);

This way, swap can be overloaded for user

4条回答
  •  梦如初夏
    2021-02-19 01:52

    the documentation of swap specifies that the idiom you refer to is common practice in the stl library

    Many components of the standard library (within std) call swap in an unqualified manner to allow custom overloads for non-fundamental types to be called instead of this generic version: Custom overloads of swap declared in the same namespace as the type for which they are provided get selected through argument-dependent lookup over this generic version.

    No such thing is present in the documentation for begin and end.

    For this reason, you can definitely use the

    using std::begin;
    using std::end;
    some_algorithm(begin(some_container), end(some_container));
    

    calling convention, but you must be aware that this is a convention which doesn't apply to e.g. standard algorithms but to your code only.

提交回复
热议问题