-
Notifications
You must be signed in to change notification settings - Fork 258
Add operator bool to modint #169
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
Conversation
Signed-off-by: szdytom <szdytom@qq.com>
mizar
commented
Aug 16, 2023
95a6273
Strange to convert zero value to true
.
For conversion rules between bool and int types in the C++ language,
https://timsong-cpp.github.io/cppwp/n4659/conv.prom#6
A prvalue of type
bool
can be converted to a prvalue of typeint
, withfalse
becoming zero andtrue
becoming one.
https://timsong-cpp.github.io/cppwp/n4659/conv.bool#1
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type
bool
. A zero value, null pointer value, or null member pointer value is converted tofalse
; any other value is converted totrue
. For direct-initialization, a prvalue of typestd::nullptr_t
can be converted to a prvalue of typebool
; the resulting value isfalse
.
@yaito3014
yaito3014
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is preferred to make conversion function explicit unless any reason not to do so.
it is preferred to make conversion function explicit unless any reason not to do so.
This is the place where we need implicit conversion. no one wants to write if ((bool)x) { ... }
anyway.
yaito3014
commented
Sep 8, 2024
it is preferred to make conversion function explicit unless any reason not to do so.
This is the place where we need implicit conversion. no one wants to write
if ((bool)x) { ... }
anyway.
You can write if (x) { ... }
even if the conversion operator to bool is marked as explicit.
cf. https://en.cppreference.com/w/cpp/language/implicit_conversion#Contextual_conversions
This repository does not accept requests for new features.
For now, we are not planning to add new features
In this case, I don't think it's necessary because you can add a custom bool cast as shown below:
#include <atcoder/modint> #include <bits/stdc++.h> struct mint : public modint998244353 { using static_modint::static_modint; mint(modint998244353 m) : modint998244353 (m) {} operator bool() const { return val() != 0; } }; int main() { mint m(123); if (m) { puts("not zero"); } else { puts("zero"); } }
It can be useful if you want something like: