C++ Logo

sg10

Advanced search

[SG10] Macro for P0458

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Fri, 11 Jan 2019 15:31:27 +0000
A colleague queried the absence of a feature-test macro for
https://wg21.link/p0458 "Checking for Existence of an Element in
Associative Containers".

I couldn't find any record of discussion in LWG review or on this
mailing list to indicate whether the omission was intentional or an
oversight.

IMHO there's not much need for a macro, based on the "if you have to
write the alternative code anyway, you might as well just do that"
principle. i.e. the macro allows you to write:

#if __cpp_lib_assoc_contains
  if (cont.contains(key))
#else
  if (cont.find(key) != cont.end())
#endif
  {
    // ...
  }

But the performance characteristics of the two forms are identical, so
checking the macro just obfuscates the code. If you need to support
old compilers, just write the code the old way.

The new contains member is more efficient than a badly-written check
for multimaps and multisets that uses "count":

#if __cpp_lib_assoc_contains
  if (cont.contains(key))
#else
  if (cont.count(key) != 0)
#endif
  {
    // ...
  }

But this code should be fixed anyway.

Does SG10 agree that we don't need a macro for p0458?

Received on 2019-01-11 16:31:40