News for hackers. Stuff that matters.
Re:Ask HN: Good C++ code bases to read? by beached_whale on Wednesday October 28, 2020 @02:30PM (#24922384)
optional has a bunch of reason's why it is better than a pointer too(it cannot be incremented, it's not a failure to initialize, it doesn't implicitly convert to things readily...). Unfortunately we don't have a monadic optional or an optional with a reference member. Both would be very useful. Value or suffers from having to evaluate the or part in all cases, but if we had .transform/.and_then/.or_else members would be really nice. Optional of a reference would allow for a whole swath of try_ methods instead of the idiomatic at( ) interface for checking bounds and retrieving in one call. at( ) suffers that it forces the choice of an exception or checking the index for being in range outside the call and then operator[] is what you want.
Re:Ask HN: Good C++ code bases to read? by CyberRabbi on Wednesday October 28, 2020 @02:39PM (#24922506)
Some monadic method would be nice but as you probably know, it’s trivial to implement one yourself.

You can store a reference in optional using reference_wrapper https://en.cppreference.com/w/cpp/utility/functional/referen...

Re:Ask HN: Good C++ code bases to read? by beached_whale on Wednesday October 28, 2020 @10:17PM (#24926378)
reference wrapper breaks in generic contexts.
Re:Ask HN: Good C++ code bases to read? by CyberRabbi on Wednesday October 28, 2020 @10:31PM (#24926458)
Could you provide an example?
Re:Ask HN: Good C++ code bases to read? by clappski on Thursday October 29, 2020 @06:55AM (#24929134)
You have to add special handling for the case that T == std::reference_wrapper<T> so you can call .get() on it to expose the underlying value. In the case of std::optional vs a pointer type (raw or smart) you can consistently use operator* to get to the underlying value. I think this is what was meant.
Re:Ask HN: Good C++ code bases to read? by beached_whale on Thursday October 29, 2020 @02:03PM (#24933260)
also if you are doing something like decltype( *val ) to get at the underlying type.