For Programmers: Free Programming Magazines  


Home > Archive > C Modurated group > November 2005 > Re: static constant in template class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: static constant in template class
Greg Herlihy

2005-11-10, 7:57 am

Serge Skorokhodov (216716244) wrote:
> Hi,
>
> I'm implementing a system, that uses template classes as an
> algorithm parameters/strategies for another template (pretty
> standard;). Each of such a strategy class needs (among other) to
> provide a floating point constant that is static by its nature.
> Something like this:
>
> // template class that is supposed to be used as strategy class
> template < ... some arguments ... >
> class StrategyOne
> {
> public:
> static double coeff()
> {
> // returns coeff value specific for StrategyOne
> }
> };
> template < ... some arguments ... >
> class StrategyTwo
> {
> public:
> static double coeff()
> {
> // returns coeff value specific for StrategyOne
> }
> };
>
> The main algorithm class template is supposed to use the templates:
>
> template < StrategyT strategy >
> class MainAlgo
> {
> ...
> void a method()
> {
> ...
>
> (some computation result) * StrategyT::coeff();
>
> ...
> }
> };
>
> There are two options to implement coeff() methods. First, the
> static member function (either defined within class definition of
> outside) can return a literal value or external constant:
>
> template <...>
> double StrategyOne<...>::coeff() { return SOME_VAL; }
>
> Defining a static member constant is another option. If I'm not
> mistaken, such a static constant must be defined outside class body:
>
> template <...>
> const double StrategyOne<...>::THE_COEFF = some_value;
>
> and static member can be defined like
> template <...>
> StrategyOne
> {
> public:
> ...
> static double coeff() { return THE_COEFF; }
> ...
> };
>
> I'm in doubt which one to prefer.
>
> 1. What implementation can produce (whatever subtle;) performance
> benefits?


The static constant declaration (with the definition in the header
file) would have both better performance and require fewer bytes than
the function call solution. In fact, I don't see any advantage to the
function call solution.

> 2. If a static member constant is is defined outside class
> definition it has to be defined in the .h file because it is a
> template class member. Can it result in some problems if the .h
> file may be included an several compilation units?


No. Templates and template member declarations can be seen by any
number of source files. The compiler (or the linker) will be sure that
the program ends up with just one.

But even a constant declaration that is not a template member can
appear in a header. For example:

const float kPi = 3.14159;

can be appear in a header file and be included by any number of source
files. There is no problem becasue const declarations have internal
linkage by default. And since a contant's value is, well, constant, it
does not matter that two different source files may have both seen the
same declaration, since they will both have also seen the same constant
value defined.

There is an additional potential benefit with the const declaration: if
the address of the constant is never taken, the compiler may never
allocate storage for it. In that case the constant declaration is as
efficient as a macro #defined value.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com