関数が複数の値を返すようにしたい場合があります。C++ でこのような動作を実現するための非常に一般的な方法の 1 つは、非 const 参照によって値を渡し、関数で値を代入することです。
void foo(int & a, int & b)
{
a = 1; b = 2;
}
どちらを使用しますか:
int a, b;
foo(a, b);
// do something with a and b
今、私はそのような関数を受け入れ、セット引数を結果を返す別の関数に転送したいファンクタを持っています:
template <typename F, typename G>
struct calc;
template <
typename R, typename ... FArgs,
typename G
>
struct calc<R (FArgs...), G>
{
using f_type = R (*)(FArgs...);
using g_type = G *;
R operator()(f_type f, g_type g) const
{
// I would need to declare each type in FArgs
// dummy:
Args ... args;
// now use the multiple value returning function
g(args...);
// and pass the arguments on
return f(args...);
}
};
このアプローチは理にかなっていますか、それともタプルベースのアプローチを使用する必要がありますか? ここでタプルベースのアプローチよりも賢いものはありますか?