Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-12-17 12:59:51


"Neal D. Becker" wrote:
>
> I hope it is permissible to ask a mp question.
>
> I'd like to have a template parameter is an int. If represents an
> arithmetic shift of an integral value. If the parameter is positive
> I'd like to shift left, and if negative shift right.
>
> Is it feasible to implement something like this? Any hints?

#include <iostream>
using namespace std;

template< int I > struct is_positive
{
   enum { value = ( I >= 0 ) };
};

template< bool, int I > struct f_impl
{
   enum { value = ( I << 1 ) };
};

template< int I > struct f_impl< false, I >
{
   enum { value = ( I >> 1 ) };
};

template< int I > struct f
{
   enum { value = f_impl< is_positive< I >::value, I >::value };
};

int main()
{
   cout << f< 42 >::value << endl;
   cout << f< -42 >::value << endl;
}

HTH, Daniel

-- 
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

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