Boost logo

Boost :

From: Hamish Mackenzie (boost_at_[hidden])
Date: 2001-11-29 20:11:04


On Thu, 2001-11-29 at 20:25, Aleksey Gurtovoy wrote:
> David Abrahams wrote:
> > as long as we're tinkering with names, consider "value_t" or
> > "value_type":
> >
> > template <typename T, T x>
> > struct value_t
> > {
> > static T const value = x;
> > };
> >
> > template <int x>
> > struct int_t : value_t<int, x> {};
> >
> > Did somebody already suggest that?
>
> Well, I thought about adding a more general integral constant wrapper to
> MPL, and implementing 'int_t' in terms of it, but I was going to call it
> 'integral_t' :). BTW, I agree with the point others made about '_t' prefix,
> so my current intention is to name them 'intergal_c' and 'int_c'
> respectively ("c" for "constant"). What do you think?

How about

template< typename Type, Type Value >
struct constant
{
  typedef Type type;
  static type const value = Value;
  operator const type &() const { return value; }
};

#ifdef BOOST_TEMPLATE_TYPEDEFS

template< int Value >
typedef constant< int, Value > int_c

#else

template< int Value >
struct int_c : public constant< int, Value >
{
  int_c( const constant< int, Value > & ) {}
};

#endif

Then it could be used like this

template< class PortType >
class net_service_traits
{
public:
  net_service_traits( const PortType & port = PortType() )
    : port_( port ) {}

  PortType port() const { return port_; }
  void port( const PortType & port ) { port_ = port; }
  // ...

private:
  PortType port_;
  // ...
};

typedef net_service_traits< int_c< 80 > >
  http_service_traits;

typedef net_server_traits< int >
  user_service_traits;

Hamish


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