$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: wb_at_[hidden]
Date: 2001-06-13 23:28:56
David Abrahams <david.abrahams_at_[hidden]> wrote on Wed Jun 13 19:51:35 2001:
| ----- Original Message -----
| From: "Luis Pedro Coelho" <deepblack9_at_[hidden]>
|
| > template <typename T, T init = T()>
| > struct auto_init {
<snip>
| >
| > What do you think?
|
| I think it's nice, but would prefer to call the template "initialized".
| Also, I note that it /only/ works with built-in integer types. What about
| pointers and floating types? I think these deserve an "initialized" type as
| well.
As is so often the case, an extra level of indirection might serve to
grant your wish.  The attached example employs a pointer-to-function as
the initializing entity; the pointee function is designed to produce
the actual initializer when provoked.
This example tests cleanly using KAI 4.0d under IRIX 6.5; I've not yet
had time to try this elsewhere and would certainly be interested to
hear of results in other environments.
        - WEB
// ======================================================================
//
// InitVia - proof-of-concept for auto-init'd values of arbitrary type
//           using a pointer-to-function as a generalized initializer.
// Inspired by copyrighted code submitted to Boost by Luis Pedro Coelho.
//
// Copyright (c) 2001 Walter E. Brown.
//
// ======================================================================
#include <iostream>
// ----------------------------------------------------------------------
// InitVia<>:
// ----------------------------------------------------------------------
template< class T, T initVal() >
class InitVia  {
public:
  InitVia() : val( initVal() )  {}
  void  reset()  { val = initVal(); }
  operator T & ()  { return val; }
  operator T const & () const  { return val; }
  InitVia( T const & other ) : val( other )  {}
  InitVia &  operator = ( T const & other )  { val = other; return *this; }
private:
  T  val;
};  // InitVia<>
// ----------------------------------------------------------------------
// Tester:
// ----------------------------------------------------------------------
class Tester  {
public:
  std::ostream &  emit( std::ostream & os )  { return os << angle; }
  void  half()  { angle /= 2.0F; }
private:
  static  float  pi()  { return 3.14159265F; }
  InitVia<float,pi>  angle;
};  // Tester
// ----------------------------------------------------------------------
// myName():
// ----------------------------------------------------------------------
char *  myName()  { static  char  me[] = "W. E. Brown"; return me; }
// ----------------------------------------------------------------------
// main():
// ----------------------------------------------------------------------
int  main()  {
  Tester  t;  t.emit( std::cout ) << std::endl;
  t.half();  t.emit( std::cout ) << std::endl;
  InitVia<char *,myName>  who;  std::cout << who << std::endl;
  who = "nobody";  std::cout << who << std::endl;
}  // main()