3 ms·
> How do you pass that into a function that can receive a pointer to foo, but does not require it? Same as you would have a function that can receive an `int`
by paulddraper 7y ago
> How do you pass that into a function that can receive a pointer to foo, but does not require it?
Same as you would have a function that can receive an `int` but does not require it. Or a receive a `std::vector` but does not require it.
I surmise that you mean "how do you pass any value (pointer or otherwise) into a function that can receive a value but does require it". And I surmise you have before used pointer indirection to pass that value because it has a conventional sentinel value of 0/NULL/nullptr.
You are correct in that optional values did not have a standardized solution, until C++17 std::optional.
If you don't have C++17, I recommend one of the equivalent third-party implementations:
* https://www.boost.org/doc/libs/1_52_0/libs/optional/doc/html/index.html https://www.boost.org/doc/libs/1_52_0/libs/optional/doc/html...
* https://github.com/akrzemi1/Optional https://github.com/akrzemi1/Optional
- deleted 7y ago[deleted]
- jcelerier 7y agostd::optional does not support references (std::optional<MyVeryLargeType&>) however unlike boost, so this precludes usage with non-copyable types.
- paulddraper 7y agoThat was decided against for now, though I'm not sure why. [1] In any case, you can use std::optional<std::reference_wrapper<MyVeryLargeType>>. [1] https://stackoverflow.com/a/26895581 https://stackoverflow.com/a/26895581
- jcelerier 7y ago> In any case, you can use std::optional<std::reference_wrapper<MyVeryLargeType>>. I would frankly give a negative code review to anyone who would do that. You go from e.g. void my_function(T* foo, T* bar, int baz) { // ... foo->stuff(bar, baz); } to void my_function(std::optional<std::reference_wrapper<T>> foo, std::optional<std::reference_wrapper<T>> bar, int baz) { // ... foo->get().call(&bar->get(), z); } this also has more overhead (sizeof(std::optional<std::reference_wrapper<T>>) is twice the size of T* ), and let's not even start talking about calling conventions and the compile time cost of having to include both <functional> and <optional> everywhere.
- paulddraper 7y agoIt's hard to tell, but if T* foo can be NULL (and I assume that it can, though there's no visible indication), your first function is gonna segfault. It's easy to tell that in the second function. Second function makes for a better code review.
- jcelerier 7y agoheh, to say that I originally wrote if(foo) and then replaced it by //... because that was not the point. Also, dereferencing an unset optional is also UB, so it would segfault all the same given the same preconditions.
- paulddraper 7y agoYes. The code is equivalent, but it's clear that the second is meant to optional. The first has no such indication, except hopefully some human-readable comment that the pointer may be NULL.
- jcelerier 7y ago> The first has no such indication, except hopefully some human-readable comment that the pointer may be NULL. I don't understand. If the parameter was not meant to be sometimes null it would be a reference; not a pointer. The fact that a pointer is used is the "this may be null" indication.