Snippet
#include <iostream>
#include <optional>
template <typename T>
struct W {
operator T&() { return *t; }
operator bool() const {
std::cout << "called W bool operatorn";
return t != nullptr;
}
T* t;
};
int main() {
int x = 42;
W<int> w{};
w.t = &x;
if (auto w_value = w) {
std::cout << "w_value " << w_value << "n";
} else {
std::cout << "w is empty" << "n";
}
return 0;
}
Code is here godboldt
What I want here is that in if (auto w_value = w)
w
should be contextually convertible to bool, such that the assignment works.
This works as expected if the line operator T&() { return *t; }
will be commented out.
But when this line is enabled, the bool
conversion operator will not be called.
Is there a way to make this code work, such that if w
converts to true
, then a reference to t
will be assigned in the if-init expression?
I have c++20 at disposal.
5
Ted has done a great job of explaining why operator bool() const
isn’t contextually called inside the if
.
Other ways to overcome that
-
define a non-
const
overloadexplicit operator bool() { std::cout << "called W bool non-const operatorn"; return t != nullptr; }
-
Use deduced-this from later C++ standard to define all the const, non-const, volatile, lvalue, and rvalue variations at once
template<typename Self> explicit operator bool(this Self&& self) { std::cout << "called deduced-W bool operatorn"; return self.t != nullptr; }
1
if (auto w_value = w)
This makes w_value
a W<int>
. What needs to be done in order for if (auto w_value = w)
to be valid one of the conversion operators must be used, so the compiler will try them out and weigh them against eachother. If both are equally valid, there is an ambiguity and the compilation will fail. If one conversion requires less changes than the other, that conversion wins.
operator bool() const
– Makes aconst_cast<const W<int>*>(this)
to make the conversion operator match.operator T&()
– Needs not to addconst
to*this
and is therefore chosen.
You can make operator T&()
explicit
if you’d like to favor operator bool() const
, which is usually good anyway since it prohibilts accidental conversions. I’d make operator bool() const
explicit
too. operator bool() const
would still be chosen in your example because of the bool
context in which is in.
The only “downside” is that you’d need to be explicit about what conversion you want when you print w_value
:
std::cout << "w_value " << static_cast<int>(w_value) << "n";
… and that is a good thing.
#include <iostream>
template <typename T>
struct W {
explicit operator T&() {
std::cout << "T&n";
return *t;
}
explicit operator bool() const {
std::cout << "called W bool operatorn";
return t != nullptr;
}
T* t;
};
int main() {
int x = 42;
W<int> w{};
w.t = &x;
if (auto w_value = w) {
std::cout << "w_value " << static_cast<int>(w_value) << 'n';
} else {
std::cout << "w is emptyn";
}
}
15
You can easily use std::optional
with just one symbol added, but it makes code significantly safer:
#include <iostream>
#include <optional>
int main() {
std::optional<int> w; // = 42;
if (auto w_value = w) {
std::cout << "w_value " << *w_value << "n";
} else {
std::cout << "w is empty" << "n";
}
}
Live example
Unfortunately implicit typecasts are quite bad in C++ especially overloaded.
2