-
Notifications
You must be signed in to change notification settings - Fork 50
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
Use std::expected for outcome when available #166
Conversation
Thanks for your contribution. I think make_unexpected() used to be necessary to make it work in C++14. Class template argument deduction is a C++17 feature. Snap's codebase is currently at C++20 so I think it is fair now to drop C++14 support. |
Sounds good, thanks for the feedback 👍 Let me know if there is anything else for me to do to get this merged. |
@@ -1,15 +1,27 @@ | |||
#pragma once | |||
|
|||
#include <version> |
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.
<version>
is a c++20 header. Can we somehow maintain compatibility with c++17?
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.
do you think we can guard it with __has_include
?
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.
Hi,
Please, you can use something like this:
#if __has_include(<version>)
#include <version>
#endif
It will make compatible with c++17.
I tested on godbolt with some compilers and it works.
Thanks.
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.
Thank you for the feedback, this makes sense. I've applied the change suggested by @paulocoutinhox and also added a #ifdef __has_include
before it, because this should make it compatible with even earlier compilers. I got the idea from the example in https://en.cppreference.com/w/cpp/feature_test , which is apparently even C++11 compatible.
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.
The code from cppreference is:
#ifdef __has_include
# if __has_include(<version>)
# include <version>
# endif
#endif
Can be this too.
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.
You mean the indentations? I don't usually use that style, so I removed it without thinking about it.
I'm trying to figure out what's usually used in this codebase. Looking at DataRef.hpp
and Future.hpp
, there are sometimes indentations for precompiler directives, but not always. And the indentations are sometimes 2, sometimes 4 spaces. And the indentations are always before the pound sign, never after. I'll happily change the indentations if someone (@LiFengSC ?) can tell me what the preferred style is.
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.
No, i mean the two directives for secure include it.
First is checking if exists __has_include: #ifdef __has_include
Second is checking the version header: #if __has_include(<version>)
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.
The code I pushed earlier is already doing that:
#ifdef __has_include
#if __has_include(<version>)
#include <version>
#endif
#endif
Or am I missing something obvious?
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.
No, you are correct, i don't see that you add #ifdef __has_include
. Sorry, my mistake.
One last request: Lets keep the
the C++20 code can directly use |
74334f0
to
fd1b314
Compare
I saw your comment via eMail and didn't realize you had posted code until I pushed my changes and checked the PR. I hope my way of reintroducing the helper is also acceptable? I also added the type_traits and utility headers, because they're technically required for I've force pushed to avoid merging fixup commits, I think the changes are small enough that this won't cause confusion. |
No problem, I like the way you did it. |
Motivation
Using
std
types when possible increases compatibility with other codebases.Considerations
make_unexpected
because it is unnecessary in my environment. Are there issues on other compilers with type deduction forunexpected
?std::expected
(ie does not define the feature flag). I can offer a godbolt link to show the basic feature check works (just switch to -std=c++20 to see that supported changes to false). I can probably create a setup to test this properly, but if someone else already has such a setup I would greatly appreciate some support.std::expected
is available. I'd argue it does so in unproblematic ways:djinni::expected
everywhere - the change should be unnoticable.std::expected
in some places anddjinni::expected
in others. This change would make any conversion code between the two types obsolete, but should not break anything, sincedjinni::expected
now just maps tostd::expected
.tl::expected
in some places anddjinni::expected
in others. This is the only problematic case, though the solution is simple: usestd::expected
ordjinni::expected
.