C++ Logo

std-proposals

Advanced search

Re: Finding the index of an element (std::find_first_index)

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Sat, 3 Oct 2020 19:02:43 +0200
Hi,

Il 01/10/20 18:56, Kosher Yosher via Std-Proposals ha scritto:
>
> Looking forward to more replies.

IMHO there are at least a few things you'd need to iron out if you want
to turn this into a viable proposal:


1. the return type. Your last post uses difference_type, which is
usually signed. OTOH, operator[] takes size_type, which is usually
signed. This means, for instance, that using the index returned in the
very same container might raise change-of-sign warnings. Is it really
wanted?

   auto idx = find_first_index_if(v, predicate);
   auto& obj = v[idx]; // warning here?


2. the return type again: how do you want to signal "not found"? npos?
optional? something else? Can you give examples about how to *use* your
function idiomatically?


3. Should this function only work for some kind of containers into which
indexing makes sense (random access?), or any range?


4. Should this function be a customization point? If yes, what's the
design exactly? Think of some hybrid container consisting of a vector
for storage + a hash table, and allowing fast lookup by index and by
value. fast_first_index (for a value, not a predicate) in such a
container should really not iterate through it.


Thanks,
-- 
Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2020-10-03 12:03:25