Re: Templates with static member in header file

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 12/04/03


Date: Thu, 04 Dec 2003 03:21:50 GMT


"Leroy van Engelen" <leroy@grimlock.student.utwente.nl> wrote...
> Say, I wanted to create a class like the following:
>
> template < typename T >
> struct Foo {
> static T *bar;
> };
>
> template < typename T > T *Foo< T >::bar = 0;
>
> And then use it like this:
>
> int n;
> Foo< int >::bar = &n;
>
> This will work ok if done within a single .cpp file. But to be really
> useful such a class should be put in a header file. However, this will
> not work: because the class is a template, its implementation should be
> completely put inside the header, but the static variable, bar, needs to
> be declared inside an object file.

I think you're overcomplicating the issue. There is no 'variable bar'.
There is only a _template_ of it. Yes, you will declare it in the header,
so damn what? It will not be instantiated until it's needed, and then,
the compiler is responsible of _merging_ all the instantiations into one
and the same (to satisfy the ODR), so, whenever you use Foo<int>::bar,
_all_ modules should refer to the same _instance_ of the static member.

Have you actually tried it, or are you just speculating that it won't work?

> This is not possible however, because
> the type (and thus the storage size) of this variable depends on the
> template argument and thus cannot be known in advance.
>
> It is possible to put the following in _one_ (no more, no less!!!) of
> the .cpp files to circumvent the problem:
>
> template <> int *Foo< int >::bar = 0;
>
> This is rather ugly: the client of the code is now exposed to
> implementation details. Not good!
>
> My question: is there nice, clean answer to this problem?
>
> I hope I've explained it good enough :)
>
> Thanks,
>
> -Leroy



Relevant Pages

  • Re: Template functions, splitting
    ... specified does NOT require all instantiations of a template function to ... So if you write (in a header) ... The compiler will create a new instantiation of doublerat Line ...
    (microsoft.public.vc.mfc)
  • using template
    ... I am using VC6 in Windows 2000. ... I got a template library. ... a separate .cpp file and included at the end of the ... I included the header in my MyPrjDoc.cpp ...
    (microsoft.public.vc.mfc)
  • Template Specialization
    ... I am using VC6 in Windows 2000. ... I got a template library. ... a separate .cpp file and included at the end of the ... I included the header in my MyPrjDoc.cpp ...
    (microsoft.public.vc.mfc)
  • Re: Non-inline template specialization of member function okay?
    ... template T Createconst; ... implemented in its CPP file: ... Member function specialization cannot be declared inside the class. ... INTERNAL COMPILER ERROR (compiler file ...
    (microsoft.public.vc.language)
  • Re: should I write code in ( *.h && *.cpp ) || only *.h
    ... I wasnt doing it for fun, its just that its my first time working on a c++ ... You can't compile a header file. ... assume you have exactly one .cpp file for your whole program. ... The separation of .h and .cpp files isn't just for fun. ...
    (microsoft.public.dotnet.languages.vc)

Loading