[mlpack-git] [mlpack/mlpack] DatasetMapper & Imputer (#694)

Tham notifications at github.com
Sun Jul 3 22:35:01 EDT 2016


> @@ -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);

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

1 : pass by value

`void DatasetMapper<PolicyType>::Policy(PolicyType policy)`

2 : pass by [universal reference](https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers)

```
inline void DatasetMapper<PolicyType>::Policy(PolicyType&& policy)
{
    policy = std::forward<PolicyType>(policy);
}
```

Q : What is std::forward?
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.

Q : Why pass by value  
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"

Q : Why pass by universal reference
A : Because this make the api self-explain and more flexible at the same time.With this api, users have more choices, example

```
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());
```

---
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/mlpack/mlpack/pull/694/files/a92afaaafb1af3deede31c1a5ef0b508bfbfe105..bace8b25ba703878a1348782e9e4feb210062a47#r69403177
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.cc.gatech.edu/pipermail/mlpack-git/attachments/20160703/bf005c0e/attachment.html>


More information about the mlpack-git mailing list