How to provide free begin/end functions for your own types

后端 未结 2 1453
既然无缘
既然无缘 2021-01-12 03:29

In one of his latest talks, Herb Sutter recommended to prefer the free begin(container) end(container) functions templates over container.beg

相关标签:
2条回答
  • 2021-01-12 04:02

    It's perfectly OK and recommended to "rely" on ADL. The begin and end functions are part of your type's interface (regardless of whether they are freestanding functions or not) and they should be in the same namespace as your type.

    0 讨论(0)
  • 2021-01-12 04:04

    I would let ADL do its work. While you are allowed to add specializations in the std namespace, there is no strong reason I can think of for doing so when a plain free function in your namespace suffices.

    In particular for the range for loops, the standard states:

    §6.5.4/1 (definition of begin-expr and end-expr):

    • otherwise, begin-expr and end-expr are begin(__range) and end(__range), respectively, where begin and end are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup, namespace std is an associated namespace.
    0 讨论(0)
提交回复
热议问题