缩短C++函数输入参数?

Sno*_*key 1 c++ armadillo c++11

我们小组最近改用C++.我的主管非常友好地提供了一个由一堆类和相关方法组成的模板.我发现的问题是大多数方法需要很多输入参数,如下所示:

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 {}
Run Code Online (Sandbox Code Playgroud)

因此,为了更好的可读性和更少的人为错误,有没有什么好方法可以在不破坏当前代码结构的情况下缩短这些输入?

我来自Python背景,我将在Python中做的是将相关输入包装在命名元组中并将其抛出到函数中.但我不知道如何在C++中应用类似的技巧.

Hen*_*nke 5

如果您阅读了文档Col,Mat您会发现

在此输入图像描述

在此输入图像描述

将它与using namespace arma;你的cpp文件结合起来(永远不会在标题中!!!)你可以做到

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 {}
Run Code Online (Sandbox Code Playgroud)

你标记了这个问题所以你没有输出参数也可以返回一个std::tuple.

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

然后你可以使用它解压缩 std::tie

std::tie(fb, fb_u, fb_uhat, fb_fhat) = ARD.boundary(n,u,uhat,fhat);
Run Code Online (Sandbox Code Playgroud)

您当然可以对输入参数执行相同的操作.