-
-
Couldn't load subscription status.
- Fork 1.1k
Changed operator bool to explicit rather than implicit conversion in Serial #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Drive-by comment: I'm concerned that this potentially breaks user code, if someone did something like this: I'm always in favor of making C++ less dangerous. But, is breaking of backwards compatibility worth it here? Is allowing Also, it seems to me that we would need to change the Additionally, how do we handle the 3rd party Cores (e.g. Teensy, ESP8266, ESP32, etc) which have implemented the |
Hopefully opening issues or pull requests on those other repositories isn't considered to be too much trouble? The time to do so is when Arduino merges this change. But don't expect any of us to actually do much at that moment. The time to "bump" the issue with another comment is when Arduino makes a non-beta release with this feature. I can't speak for the ESP cores, but I can tell regarding Teensy... I do pay attention to Arduino development. I generally see changes like this long before they're merged. :-) |
|
@bxparks actually that example will not break. This example would run and work because this is not an implicit conversion but rather a form of checking a condition. So don't worry... None of yours or the existing communities code would end up breaking!!!! I also plan on adding this Pull Request on Multiple Arduino based Repos. |
|
@bxparks updated with support for all 4 |
If you were to notice that in every Serial based Class, we have a function with the type
operator bool() { return true; }By writing explicit, we can ensure that there is no form of implicit conversion or otherwise.
For further details view https://stackoverflow.com/questions/39995573/when-can-i-use-explicit-operator-bool-without-a-cast
This would ensure that
bool b = Serial1;would not result in b getting a valueFor example, comparing the following codes:-
Does not result in an error due to an absence of explicit.
However, adding explicit we get an error that the conversion cannot be performed in the following sample.
An Operator bool with explicit would render it compatible with only these statements
For example
CONSTEXPR
Further, I have marked these statements as
constexprgiven they indeed are constant compile time expressions and their values will not change dynamically at runtime