Confusion with bool conversion on absl::optional<bool>

234 views
Skip to first unread message

Yuwei Huang

unread,
Nov 6, 2021, 12:18:24 AM11/6/21
to cxx
The bool operator on abs::optional returns true as long as it holds a value, which can be quite confusing when people instantiate it with bool, say:

absl::optional<bool> some_bool = false;
if (some_bool) {
  LOG(INFO) << "Hey!";  // This will be logged.
}

This could be especially problematic if the optional is defined in some header file and the developer uses it without realizing it's an optional instead of a plain old bool.

I wonder if it makes sense to add some presubmit warning for using the bool operator on absl::optional<bool>, or maybe just ban it altogether and force people to use has_value() or value() instead? 

Aaron Tagliaboschi

unread,
Nov 6, 2021, 12:45:07 AM11/6/21
to Yuwei Huang, cxx
I'd really just question the introduction of an optional boolean on review. Is there an example of this in Chromium? I'm really struggling to come up with an example where it would be useful.

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/f19ee7ea-793c-478f-a452-abd570d3be57n%40chromium.org.

Sylvain Defresne

unread,
Nov 6, 2021, 5:02:00 AM11/6/21
to Aaron Tagliaboschi, Yuwei Huang, cxx
There is usage of absl::optional<bool> in Chromium such as base::Value::FindBoolKey (and similar functions). The goal is to allow returning a base::nullopt value if the key is not found, or a bool with the correct value otherwise. I think this is a valid use case for absl::optional<bool> (though error prone).

The subject has already been discussed on this list in past thread, the summary was:
1. if possible use an enum with three possible values "undefined", "yes", "no" if possible,
2. prefer using `has_value()` or `value_or(default)` instead of dependending on the conversion, and limit scope of the optional,
3. google style and internal google code do not ban optional<bool> though they point they are error prone.

I think having a clang plugin warning for the use of `absl::optional<bool>::operator bool() const` would be a good idea since we have alternatives (.has_value() / .or_value(default)). I am not sure a presubmit would be possible as there are valid use cases for absl::optional<bool>.
-- Sylvain

Yuwei Huang

unread,
Nov 6, 2021, 6:52:29 PM11/6/21
to Sylvain Defresne, Aaron Tagliaboschi, Yuwei Huang, cxx
Yeah, I wouldn’t ban the usage of optional<bool>, but having something that warns about the usage of operator bool() would be good.

Ryan Hamilton

unread,
Nov 6, 2021, 6:54:29 PM11/6/21
to Yuwei Huang, Aaron Tagliaboschi, Sylvain Defresne, cxx
Agreed. Optional<bool> seems useful if uncommon, but operator bool() in this case seems dangerous to me. 

Lei Zhang

unread,
Nov 7, 2021, 11:57:01 AM11/7/21
to Yuwei Huang, cxx
This topic came up a few months ago on chromium-dev:
https://groups.google.com/a/chromium.org/g/chromium-dev/c/F43x0MO_tEU/m/AmBbCdxxAQAJ

Danil Chapovalov

unread,
Nov 8, 2021, 7:40:43 AM11/8/21
to Lei Zhang, Yuwei Huang, cxx

Ryo Hashimoto

unread,
Nov 8, 2021, 9:55:26 PM11/8/21
to Danil Chapovalov, Lei Zhang, Yuwei Huang, cxx
+1 for having a warning against operator bool().
Even when I know it's an optional<bool>, it's really difficult to avoid this confusion when reading the code.

Also it might be worth having the same warning for optional<T*> and other types that frequently get converted to bool (though I guess it happens much less frequently compared to optional<bool>).

Gabriel Charette

unread,
Nov 10, 2021, 9:49:34 AM11/10/21
to Ryo Hashimoto, Danil Chapovalov, Lei Zhang, Yuwei Huang, cxx
Agreed that optional<T> and in particular optional<T*> has the same problem really. I think it comes down to variable naming? i.e. Don't call an optional<bool> "is_enabled" but rather "enabled_state" or "optional_status" and ideally use StrongAlias to qualify the bool as an explicit type.

Reply all
Reply to author
Forward
0 new messages