How do I create a const boost::iterator_range

后端 未结 1 1625

The comment at Why does boost::find_first take a non-const reference to its input? suggests \"the caller to create a non-const iterator_range with const_iterator template pa

1条回答
  •  梦毁少年i
    2021-01-14 18:07

    Having non-const references to the range avoids binding to temporaries ¹

    I'd avoid your conundrum² by letting the compiler do your work:

    tMyMap const& my_map; // NOTE const
    // ...
    
    return boost::make_iterator_range(my_map.lower_bound(123), mymap.upper_bound(456));
    

    ¹ Standard C++ extends lifetimes of temporaries bound to const-reference variables, but this doesn't apply to references bound to object members. Therefore, aggregating ranges by reference is very prone to this mistake.

    /OT: IMO even with the precautions/checks some Boost Range features (like adaptors) are frequently too unsafe to use; I've fallen into those traps more often than I care to admit.

    ² apart from the fact that we cannot reproduce it from the sample you gave

    0 讨论(0)
提交回复
热议问题