[kde-edu]: Templates or not ?

Vladimir ks.vladimir at gmail.com
Mon Nov 19 19:00:42 CET 2007

Hello Benoit !

In my case I have only two specializations, but I can't hide the templates 
inside the library because:
- the interfaces should be specialized too, for example Particle2d::position() 
should return Vector2d but Particle3d::position() should return Vector3d;
- currently I don't use d-pointers and for some classes I think its good (for 
performance reasons), so private members are declared in headers.

Probably I can instantiate all classes for 2d and 3d in sources so that the 
library will contain binary code for this two versions ? But will it be 
meaningfull taking into account that almost all code will be in headers 
anyway ?

Now I'm thinking that headers-only library is not so bad: at least I can 
forget about problems with binary compatibility ;-) And as you suggested I 
can use templates to implement optional features.

The problem with the static data members is that they should be declared in 
only one source file so that there will be only one copy of them when linking 
(AFAIK). So I do not know where should I declare them in header-only library. 
Are there any workaround ?

Thanks a lot for your reply !

> Looking forward to talk to you in Paris,
Me too ! It's going to be a very interesting meeting for me ! I only hope that 
there will be no problems with my visa.

      Best Regards,

On Monday 19 November 2007 19:26:50 Benoit Jacob wrote:
> Hi Vladimir,
> It depends. How many different possible template specializations will be
> used? If there are only two (2d and 3d) then you can have your templated
> code internal to your library, specialize it from inside your library, and
> thus still make a binary library.
> The cases where it makes more sense to go templated are:
> - when the number of possible instanciations is higher (e.g. in Eigen, the
> numbers of rows/columns can be 1,2,3,4,... or dynamic, and the base type
> can be
> int,float,double,complex<...>)
> - when you want to expose a lot of functionality which might not be
> actually used, and you don't want the unused stuff to inflate the binary
> size (e.g. in Eigen, we'll happily code a lot of advanced things without
> having to ponder whether it's worth the binary size, as templated code
> only gets compiled when it's used).
> - when you want to achieve stuff that can only be achieved by template
> techniques (e.g. the expression templates in Eigen2).
> About your introspection system, I don't see why static data members would
> be incompatible with templates. I think there's no problem at all. Once
> it's compiled, templated code becomes just ordinary binary code!
> Looking forward to talk to you in Paris,
> Benoit
> On Mon, 19 Nov 2007, Vladimir wrote:
> > Hello All,
> >
> > I'm thinking (still only in theory) about implementing 3d support for
> > StepCore. A very big part of the library is almost
> > dimensional-independent: I just need to replace Vector2d by Vector3d and
> > the obvious way to do it is to use templates. But AFAIK this makes my
> > library header-only which increases compilation time and disallows
> > sharing of the library. Moreover, in StepCore I'm using my own object
> > introspection system which needs static data members and I do not know
> > how to implement them in header-only library. Is it possible ? Probably
> > someone has some other suggestions ?
> >
> > --
> >      Best Regards,
> >        Vladimir
> > _______________________________________________
> > kde-edu mailing list
> > kde-edu at mail.kde.org
> > https://mail.kde.org/mailman/listinfo/kde-edu
> _______________________________________________
> kde-edu mailing list
> kde-edu at mail.kde.org
> https://mail.kde.org/mailman/listinfo/kde-edu

More information about the kde-edu mailing list