You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@mesos.apache.org by "Mark Wang (JIRA)" <ji...@apache.org> on 2015/05/27 08:03:17 UTC

[jira] [Issue Comment Deleted] (MESOS-2716) Add non-const reference version of Option::get.

     [ https://issues.apache.org/jira/browse/MESOS-2716?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Mark Wang updated MESOS-2716:
-----------------------------
    Comment: was deleted

(was: I see at line 106 has this same functionality with getOrElse(). shall we just rename it?
{noformat}// This must return a copy to avoid returning a reference to a temporary. 
T get(const T& _t) const { return isNone() ? _t : t; } {noformat})

> Add non-const reference version of Option<T>::get.
> --------------------------------------------------
>
>                 Key: MESOS-2716
>                 URL: https://issues.apache.org/jira/browse/MESOS-2716
>             Project: Mesos
>          Issue Type: Improvement
>          Components: stout
>            Reporter: Benjamin Mahler
>            Assignee: Mark Wang
>              Labels: newbie
>
> Currently Option only provides a const reference to the underlying object:
> {code}
> template <typename T>
> class Option
> {
>   ...
>   const T& get() const;
>   ...
> };
> {code}
> Since we use Option as a replacement for NULL, we often have optional variables that we need to perform non-const operations on. However, this requires taking a copy:
> {code}
>     static void cleanup(const Response& response)
>     {
>       if (response.type == Response::PIPE) {
>         CHECK_SOME(response.reader);
>         http::Pipe::Reader reader = response.reader.get(); // Remove const.
>         reader.close();
>       }
>     }
> {code}
> Taking a copy is hacky, but works for shared objects and some other copyable objects. Since Option represents a mutable variable, it makes sense to add non-const reference access to the underlying value:
> {code}
> template <typename T>
> class Option
> {
>   ...
>   const T& get() const;
>   T& get();
>   ...
> };
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)