Re: tuple_max in C++1x
On 11 Apr., 08:01, PGK wrote:
Is it possible to find the maximum value of a C++1x tuple,
You mean C++0x.
and then update that value?
Yes. You could unroll the loop via metaprogramming/recursion and
return a reference to the max element -- provided that the element
types are all the same. You chould start like this (untested):
#include <tuple>
#include <utility>
template<int Remaining>
struct tuple_for_each_helper
{
template<typename Tuple, typename Func>
static void doit(Tuple & tup, Func & f)
{
const int ts = std::tuple_size<Tuple>::value;
f( std::get<(ts-Remaining)>(tup) );
tuple_for_each_helper<(Remaining-1)>::doit(tup,f);
}
};
template<>
struct tuple_for_each_helper<0>
{
template<typename Tuple, typename Func>
static void doit(Tuple & tup, Func & f) {}
};
template<typename Tuple, typename Func>
Func tuple_for_each(Tuple & tup, Func f)
{
const int ts = std::tuple_size<Tuple>::value;
tuple_for_each_helper<ts>::doit(tup,f);
return std::move(f);
}
The rest follows from that. For example, you might want to use
tuple_for_each with a lambda expression. Alternativly, you could use
std::array instead of std::tuple. std::array allows indexing with
"runtime values".
Cheers,
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]