-
Notifications
You must be signed in to change notification settings - Fork 252
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
[SUGGESTION] optional-like API for raw-pointers (thanks to UFCS) #192
Comments
Adding additional methods for template < typename X, typename D >
constexpr bool has_value( std::unique_ptr<X, D> const& x ) noexcept {
return bool(x);
}
template < typename X, typename D >
constexpr auto value( std::unique_ptr<X, D>& x ) -> decltype(*x) {
if (!x)
throw std::bad_optional_access();
return *x;
}
template < typename X, typename D >
constexpr auto value( const std::unique_ptr<X, D>& x ) -> decltype(*x) {
if (!x)
throw std::bad_optional_access();
return *x;
}
template < typename X, typename D, class U >
constexpr auto value_or( std::unique_ptr<X, D> const& x, U&& default_value ) -> CPP2_TYPEOF(*x) {
if (!x)
return std::forward<U>(default_value);
return *x;
}
template < typename X, typename D, class... Args >
auto emplace( std::unique_ptr<X, D>& x, Args&&... args ) -> decltype(*x) {
using T = CPP2_TYPEOF(*x);
x.reset(new T(std::forward<Args>(args)...));
return *x;
} Made the below code work as well: uptr := new<int>(88);
std::cout << "uptr.has_value(): " << std::boolalpha << uptr.has_value() << std::endl;
std::cout << "uptr.value(): " << uptr.value() << std::endl;
std::cout << "uptr.value_or(0): " << uptr.value_or(0) << std::endl;
uptr.reset(); std::cout <<"\nuptr.reset();\n" << std::endl;
std::cout << "uptr.value_or(0): " << uptr.value_or(0) << std::endl;
uptr.emplace(123); std::cout <<"\nuptr.emplace(123);\n" << std::endl;
std::cout << "uptr.emplace(9999): " << uptr.emplace(9999) << std::endl;
std::cout << "uptr.value_or(0): " << uptr.value_or(0) << std::endl; |
To me, anything that makes the " |
Thanks for the suggestion. One question I have is that the Cpp2
So I think I'd prefer to stick with unifying under If you still feel strongly about pursuing this suggestion, could you recast it in the form of the suggestion issue template please? In particular, is this something that's proven that we already teach people to do in today's C++ guidance, and so we'd be making a known best practice the default? What current guidelines would it let us no longer teach, or make easier to teach? Thanks! |
@filipsajdak I see you thumbs-up'd my comment, so I'll close this as resolved for now -- if you're planning to pursue it with the suggestion issue template's additional information just go ahead and create it as a new issue with the additional info and rationale. Thanks! |
Yes, close it. I found that and shared it. If I find more facts to back it up, I will repost it with the proper template. |
Should it?
|
Thanks to UFCS, we can create an API that will make working with pointers more like with
std::optional
. That can make it easier to avoid dereferencing null pointers. Also, it can help create more generic code that will work with pointers andstd::optional
.The above code will return:
We can also make overloads for smart pointers.
The text was updated successfully, but these errors were encountered: