Boost logo

Boost :

From: Ullrich Koethe (koethe_at_[hidden])
Date: 2000-11-01 13:13:57


Eric Weitzman wrote:
>
> [Intro to Advanced C++ Template Techniques 101.]
>
> Is there a a technique for defining a static data member for a template
> class other than using some macro kludge or requiring manual code inserted
> into an implementation file? For example
>
> in C.h:
>
> template <class T>
> class C {
> static int i_;
> };
>
> #define DEFINE_STATIC_C(_t) int C<_t>::i_ = 0;
>
> in C.cpp:
>
> typedef C<blah> MyC;
> int MyC::i_ = 0; // how can one avoid doing this?
> int C<blah>::i_ = 0; // ... or this?
> DEFINE_STATIC_C(blah); // ... or this?
>

You may use a static member function containing a static variable, like
this:

      template <class T>
      class C {
          static int & i_()
          {
              static int i;
              return i;
          }
      };

C<Foo>::i_() can be used like a normal static data member, modulo
syntax.

Ulli

-- 
 ________________________________________________________________
|                                                                |
| Ullrich Koethe  Universität Hamburg / University of Hamburg    |
|                 FB Informatik / Dept. of Computer Science      |
|                 AB Kognitive Systeme / Cognitive Systems Group |
|                                                                |
| Phone: +49 (0)40 42883-2573                Vogt-Koelln-Str. 30 |
| Fax:   +49 (0)40 42883-2572                D - 22527 Hamburg   |
| Email: u.koethe_at_[hidden]               Germany             |
|        koethe_at_[hidden]                        |
| WWW:   http://kogs-www.informatik.uni-hamburg.de/~koethe/      |
|________________________________________________________________|

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