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

后端 未结 2 1454
既然无缘
既然无缘 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: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.

提交回复
热议问题