Boost logo

Boost Users :

From: Lang Stefan (SLang_at_[hidden])
Date: 2008-03-03 04:13:58


Hi,

As I am not overly familiar with boost or it's mpl library yet, I cannot
provide any specific advice along these lines.

According to your description, my advice would be to create helper
template classes or functions that rely only on some of the template
parameters of your actual template class, but not N. For these helper
functions and classes you do not need partial specialization, and thus
no duplicate code. Depending on the complexity of your functions you
might also extract those parts that do not depend on N into more
template functions. If these helpers need access to private members of
your makeTree class, you can declare them friend. (you might need to
replicate those friend declarations in your partial specializations)

For those parts of the code that do depend on N, but do not need a
different implementation, you may still provide templated helper
classes. The compiler will still generate multiple class implementations
for different values of N, but you won't need to replicate the actual
code yourself.

Hope this helps.

Cheers,
Stefan

> ------------------------------
>
> Message: 6
> Date: Sat, 1 Mar 2008 14:38:34 +0100
> From: "Hicham Mouline" <hicham_at_[hidden]>
> Subject: [Boost-users] templates: parts of functions
> To: <boost-users_at_[hidden]>
> Message-ID: <000001c87ba1$8cfe0d30$a6fa2790$@org>
> Content-Type: text/plain; charset="us-ascii"
>
> Hi,
>
>
>
> I have a function makeTree whose behavior depends on 3
> different policies
>
> typename T1
>
> typename T2
>
> int N
>
>
>
> For performance reasons, I need to write the 2- and 3- specializations
> alongside the general N case. That is, the 2- and 3- default
>
> instantiations generated from the N case by the compiler are
> not optimal.
>
>
>
> As it is impossible to write function templates partial
> specializations, I
> encapsulate the
>
>
>
> template< typename T1, typename T2, int N> void
> makeTree(Head<N>&, const
> T2&, const C&) ;
>
>
>
> function in a class template
>
>
>
> template< typename T1, typename T2, int N> class makeTree{
>
> void makeTree(Head<N>&, const T2&, const C&);
>
> };
>
> and write the partial specializations <T1,T2,2> and <T1,T2,3>.
>
>
>
>
>
> Now, this makeTree function is quite large. It includes code that :
>
> . doesn't depend on any of the template arguments
>
> . code that depends only T1
>
> . code that depends only on T2
>
> . code that depends only on N
>
> . code that depends only on <T1,N>.
>
>
>
> I am worried about code bloat, and the effect on overall
> performance of the
> actual code generated even if it is not run
>
> (I noticed substantial effects depending on the size of
> object files and
> final shared library generated).
>
>
>
> Besides this, I suppose it is a better design to have these
> segments of code
> put in template functions
>
> with the right template arguments.
>
>
>
> Is it MPL the library that I should look into for such work?
>
> Perhaps nothing in Boost particularly facilitates this work,
> and std c++
> templates are enough,
>
>
>
> Thoughts, comments are appreciated,
>
>
>
> Rds,
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net