Boost logo

Boost :

From: Hervé Brönnimann (hervebronnimann_at_[hidden])
Date: 2008-05-14 22:35:32


That would also have been my implementation:

template <int N>
struct positive_power
{
     template <typename T>
     static typename tools::promote_args<T>::type result(T base)
     {
          typename tools::promote_args<T>::type power =
positive_power<N/2>::result(base);
          return (N%2) ? base * power *power : power * power;
     }
};

with the two specializations:

template <>
struct positive_power<0>
{
     template <typename T>
     static typename tools::promote_args<T>::type result(T base)
     {
          return 1;
     }
};

template <>
struct positive_power<1>
{
     template <typename T>
     static typename tools::promote_args<T>::type result(T base)
     {
          return base;
     }
};

  (strictly speaking, <0> only is needed, but the compiler may not
know to optimize multiplying by T(1), which is one criticism of
previous post by John Moeller, who proposes essentially the same
solution but with a factor ((N%2) ? base : 1) that I don't like).

Johan: what's wrong with it?

--
Hervé Brönnimann
hervebronnimann_at_[hidden]
On May 14, 2008, at 1:49 PM, Johan Råde wrote:
> John Maddock wrote:
>> Steven Watanabe wrote:
>
>>>        return (N%2) ? base*positive_power<N-1>::result(base)
>
> Shouldn't positive_power<N>(base) be reduced to
> square(positive_power<N/2>(base)
> or
> base * square(positive_power<N/2>(base))
> depending on N%2?
>
> --Johan
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/ 
> listinfo.cgi/boost

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk