Prev: rvalue-reference result premature destruction
Next: draft standard n3090.pdf, std::unique() algorithm
From: PGK on 10 Apr 2010 15:01 Is it possible to find the maximum value of a C++1x tuple, and then update that value? I could almost imagine calculating the maximum value in a tuple, but the index corresponding to this value would not be a compile-time constant literal, as required by the tuple "get" function. Graham. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 11 Apr 2010 05:01 On 11 avr, 07:01, PGK <graham.k...(a)gmail.com> wrote: > Is it possible to find the maximum value of a C++1x tuple, and then > update that value? Yes. > I could almost imagine calculating the maximum > value in a tuple, but the index corresponding to this value would not > be a compile-time constant literal, as required by the tuple "get" > function. Well obviously, you need a meta-programming loop, not a normal loop. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Pete Becker on 11 Apr 2010 05:01 PGK wrote: > Is it possible to find the maximum value of a C++1x tuple, It's impossible to say. There is no work being done on C++1x. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference" (www.petebecker.com/tr1book) [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: PGK on 12 Apr 2010 06:41 > Is it possible to find the maximum value of a C++1x tuple, and then > update that value? The code below will find the index of the max value of a tuple of elements supporting the lessthan operator. The problem is, how do I use this to update that tuple value; say set it to zero? (The const int I obtain cannot be used in the tuple "get" function as it cannot appear in a constant expression.) template <typename T_Tuple,size_t size> struct max_helper { static const int tup_max2( T_Tuple &t, double cur_max, int &index ) { if (get<size-1>(t) > cur_max) { cur_max = get<size-1>(t); index = size-1; } max_helper<T_Tuple,size-1>::tup_max2(t,cur_max,index); return index; } }; template <typename T_Tuple> struct max_helper<T_Tuple,0> { static const int tup_max2( T_Tuple &t, double cur_max, int &index ) { return index; } }; int main(int argc, char *argv[]) { int i = -1; double cur = -100.0; tuple<int,double,long,int,float> t(3,5,2,14,7); // max is at index 3 const int index = max_helper<decltype(t), tuple_size<decltype(t)>::value> ::tup_max2(t,cur,i); std::cout << "max value is at index: " << i << std::endl; // get<index>(t); // error return 0; } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: SG on 12 Apr 2010 06:40 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! ]
|
Next
|
Last
Pages: 1 2 Prev: rvalue-reference result premature destruction Next: draft standard n3090.pdf, std::unique() algorithm |