1

Our group recently switched to C++. My supervisor is kind enough to provide a template which consists of a bunch of classes and relevant methods. The problem I found is that most methods require a lot of input parameters, like this:

void AdvectionReactionDiffusion::boundary(const arma::Col<double>& n, const arma::Col<double>& u, const arma::Col<double>& uhat, const arma::Col<double>& fhat, arma::Col<double>& fb, arma::Mat<double>& fb_u, arma::Mat<double>& fb_uhat, arma::Mat<double>& fb_fhat) const {}

So, for the sake of better readability and less human mistakes, is there any good ways to shorten these inputs without breaking the current structure of the code?

I come from a Python background, and what I will do in Python is wrap relevant inputs in a named tuple and throw it at the function. But I have no idea of how to apply the similar trick in C++.

Justin
  • 24,288
  • 12
  • 92
  • 142
Snow Nickey
  • 33
  • 1
  • 4
  • 7
    Shouldn't you be asking your *supervisor* (teacher?) that question? – Ken White Jun 08 '17 at 02:12
  • Are you wanting to provide named optional arguments? Separate the argument types from the function signature? Forward groups of parameters to another function? – Ben Voigt Jun 08 '17 at 02:14
  • That function has 4 _out_ parameters but returns nothing! It might be time to just see if you can break the function up – Tas Jun 08 '17 at 02:15
  • 2
    To shorten a function or method parameter list, put the parameters into a structure and pass the structure. – Thomas Matthews Jun 08 '17 at 02:16
  • 1
    This is what `struct/class` does, basically you should always try to group things and pass the reference of that group in the funciton. I know that this could not be your case or sometimes could be difficult but you still can re-think about your code in this way. – Pushpendra Jun 08 '17 at 02:31
  • @KenWhite I'd like to but my supervisor is on vacation. So I figured here is the best place to ask. – Snow Nickey Jun 08 '17 at 03:35
  • Obviously, the contents of `fb`, `fb_u`, `fb_uhat` and `fb_fhat` want to form a single class. Same for `u` and `uhat`. That alone will reduce the number of arguments to 4. Futhermore the last parameter is now a return type, so the function has really only three input parameters (`n`, `u` and `fhat`). – alfC Jun 08 '17 at 03:36

1 Answers1

5

If you read the docs on Col and Mat you will find

enter image description here

enter image description here

Combining this with using namespace arma; in your cpp file (never in the header!!!) you can do

void AdvectionReactionDiffusion::boundary(const vec& n,
                                          const vec& u,
                                          const vec& uhat,
                                          const vec& fhat,
                                          vec& fb,
                                          mat& fb_u,
                                          mat& fb_uhat,
                                          mat& fb_fhat) const {}

You tagged this question so instead of having output parameters you could also return a std::tuple.

std::tuple<vec,mat,mat,mat>
AdvectionReactionDiffusion::boundary(const vec& n,
                                     const vec& u,
                                     const vec& uhat,
                                     const vec& fhat) const {}

which you can then unpack using std::tie

std::tie(fb, fb_u, fb_uhat, fb_fhat) = ARD.boundary(n,u,uhat,fhat);

You can of course do the same to the input parameters.

Henri Menke
  • 10,705
  • 1
  • 24
  • 42
  • [Don't write using namespace](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) on any non-trivial project – Passer By Jun 08 '17 at 02:46
  • @PasserBy I often do that, but only in local scope. Otherwise writing mathematical expressions with lots of `std::sin` and `std::exp`, etc would be really painful to type. – Henri Menke Jun 08 '17 at 02:48
  • I agree that the typing is tedious, but I personally just view this as a defect of c++. I'd say the scope resolution operator would be so much less painful if it were not **two** freaking colons that **requires pressing shift** – Passer By Jun 08 '17 at 03:11