<p>In <a href="https://github.com/mlpack/mlpack/pull/694#discussion_r69403177">src/mlpack/core/data/dataset_info_impl.hpp</a>:</p>
<pre style='color:#555'>> @@ -127,6 +125,13 @@ inline PolicyType& DatasetMapper<PolicyType>::Policy()
> return this->policy;
> }
>
> +template<typename PolicyType>
> +inline void DatasetMapper<PolicyType>::Policy(PolicyType& policy)
> +{
> + this->policy = std::move(policy);
</pre>
<p>Pass by reference do not tell the user you want to move the value policy, this may give user a surprise like "oops, I do not meant to change the value of policy, but after calling it, the data of my parameter loss". To make the api more self-explain, we could</p>
<p>1 : pass by value</p>
<p><code>void DatasetMapper<PolicyType>::Policy(PolicyType policy)</code></p>
<p>2 : pass by <a href="https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers">universal reference</a></p>
<pre><code>inline void DatasetMapper<PolicyType>::Policy(PolicyType&& policy)
{
policy = std::forward<PolicyType>(policy);
}
</code></pre>
<p>Q : What is std::forward?<br>
Ans : This function will convert the parameter with universal reference type to rvalue if the parameter is rvalue, to lvalue if the parameter is lvalue.</p>
<p>Q : Why pass by value<br><br>
A : Because this would not have any side effect on the parameter(in most case) you pass in, this make codes easier to manage, user will never have a surprise like "oops, my parameter changed by the function"</p>
<p>Q : Why pass by universal reference<br>
A : Because this make the api self-explain and more flexible at the same time.With this api, users have more choices, example</p>
<pre><code>MissingPolicy mp;
//do something......
//do not want to move
mapper.Policy(mp)
//want to move
mapper.Policy(std::move(mp));
//or construct the policy directly, this will move too
mapper.Policy(MissingPolicy());
</code></pre>
<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/mlpack/mlpack/pull/694/files/a92afaaafb1af3deede31c1a5ef0b508bfbfe105..bace8b25ba703878a1348782e9e4feb210062a47#r69403177">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe/AJ4bFA15yXyHaA6uuP3W-D9aVzf6t3gRks5qSHFVgaJpZM4I07W-">mute the thread</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AJ4bFMV9J9UHwa_B-SHZ-JsI7j0VIR8wks5qSHFVgaJpZM4I07W-.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
<link itemprop="url" href="https://github.com/mlpack/mlpack/pull/694/files/a92afaaafb1af3deede31c1a5ef0b508bfbfe105..bace8b25ba703878a1348782e9e4feb210062a47#r69403177"></link>
<meta itemprop="name" content="View Pull Request"></meta>
</div>
<meta itemprop="description" content="View this Pull Request on GitHub"></meta>
</div>