0

Suppose we have following C++ code:

auto found = std::lower_bound(container.begin(), container.end(), val);
return found != container.begin() && found != container.end();

How can I tell from a debugger which operand of && operator was false -- a left one or a right one, or actually both?

I wish I could use print, but for some reason it fails:

(gdb) p found != container.end()
Cannot resolve function operator!= to any overloaded instance
i cant
  • 401
  • 2
  • 9
  • Might it be that the debugger couldn't deduce the type of `found`? What happens if you just print `found`? – thedemons Oct 27 '22 at 16:15
  • 1
    Step into the 1st call call put a breakpoint on the return, go to breakpoint, inspect the result that is going to be returned. Repeat if needed for 2nd call. – Richard Critten Oct 27 '22 at 16:17
  • In your sample code you mention `container.end()` but in your sample GDB inspectino you mention `components.end()`. – Nathan Pierson Oct 27 '22 at 16:21
  • @NathanPierson oops, sorry! I forgot to edit a variable name when asked this question, fixed it. – i cant Oct 27 '22 at 16:24
  • Related/dupe: [gdb Could not find operator[\]](https://stackoverflow.com/questions/24130093/gdb-could-not-find-operator) – Jason Oct 27 '22 at 16:27
  • @JasonLiam it is maybe related, but certainly not a dupe. – i cant Oct 27 '22 at 16:29
  • @JasonLiam what if some of the operands contains a function call which has a side effect. We cannot call `print` with such function from gdb without messing up program's state, so the need of knowing operand's result is mandatory here. I require to reopen this question, because your "solution" is simply not applicable – i cant Oct 27 '22 at 16:32
  • Since the OP tagged lldb, it seems marginally worth pointing out that this expression runs successfully in the latest version of lldb w/o having to force the generation of template functions or other tricks. At least that's true with `container` either a `vector` or `set`. So it seems like gdb should be able to evaluate this one as well. – Jim Ingham Oct 27 '22 at 17:03
  • For some containers, `p found._M_current != container.end()._M_current` might work. – ssbssa Oct 27 '22 at 17:31
  • Am I the only one thinking that this specific predicate doesn't make much sense? – n. m. could be an AI Oct 28 '22 at 05:56

1 Answers1

0

I would suggest simply introducing two local bool variables, and then you can look at them as needed:

auto found = std::lower_bound(container.begin(), container.end(), val);
bool isBegin = (found == container.begin());
bool isEnd = (found == container.end());
return !(isBegin || isEnd);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Yeah, that would actually work, but debugging is already a very tedious procedure, and recompiling and restarting my binary over and over again makes it even more tedious, so I'm looking for way to avoid that. – i cant Oct 27 '22 at 16:28