Re: template metaprogram to handle max(a,b,c)?
On Sep 30, 9:03 pm, Helmut Jarausch <jarau...@skynet.be> wrote:
Hi,
given a function , e.g.
double max(double,double),
is it possible to write a template metaprogram MAX, which
transforms
MAX(a,b,c) into max(max(a,b),c)
and similarly for any number of arguments.
Not with the syntax you want, at least until we get variadic templates
and parameter packs in C++09. It is certainly possible to implement
something like "max(a)(b)...(z)" by overloading operator(), e.g.:
// definition
struct {
template <class T>
struct helper {
const T val;
helper(const T& val): val(val) { }
operator const T& () const { return val; }
helper operator() (const T& newval) const { return
helper(std::max(val, newval)); }
};
template <class T>
helper<T> operator() (const T& val) const { return helper<T>(val); }
} const max_of;
// usage
int m = max_of(123)(456)(789);
Tthere's no need for any template metaprogramming here either - a
decent compiler should be able to inline all the nested calls.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]