[Insight-developers] help with template function problem

Brad King brad.king@kitware.com
Thu, 18 Oct 2001 11:47:16 -0400 (EDT)


The approach suggested by Aljaz is probably better for a global function
than my Dim<dim> work-around.  I use it in several places in the CABLE
wrapper internals:

template<unsigned int VNumber>
struct A
{
  static void tfunction( )
    {
    unsigned int temp = VNumber;
    std::cout << temp << std::endl;
    }
};

A<4>::tfunction();

However, my work around (shown below from Jim's message) is needed when
the function is a member of a class.  Then, introducing a nested template
class would prevent the function from having access to the class's
members.

-Brad

> struct DimBase {};
> 
> template <unsigned int VNumber>
> struct Dim : public DimBase {};
> 
> template<unsigned int VNumber>
> void tfunction(Dim<VNumber>& foo )
> {
>   unsigned int temp = VNumber;
>   std::cout << temp << std::endl;
> }
> 
> 
> void function( unsigned int num )
> {
>   switch (num)
>     {
>     case 1:
>       tfunction(Dim<1>());
>       break;
>     case 2:
>       tfunction(Dim<2>());
>       break;
>     case 3:
>       tfunction(Dim<3>());
>       break;
>     case 4:
>       tfunction(Dim<4>());
>       break;
>     }
> 
> }