[Insight-developers] help with template function problem

Miller, James V (CRD) millerjv@crd.ge.com
Thu, 18 Oct 2001 08:15:27 -0400


Opps, below I meant to say that DimBase is NOT needed.  Again it is
useful as a catchall for a default (non-specialized) method.


-----Original Message-----
From: Miller, James V (CRD) 
Sent: Thursday, October 18, 2001 8:07 AM
To: 'lng@insightful.com'; Insight-developers (E-mail)
Subject: RE: [Insight-developers] help with template function problem


Lydia,

I believe this is a known problem with the VC++.  Brad King developed
a work around last spring. His original email should be in the archive.
He checked in a test that illustrates this technique in 
Testing/Code/BasicFilters/itkFilterDispatchTest.cxx.  
I used his approach in the JoinImageFilter to switch function implementations
based on whether the filter was given scalar or vector data.  Below is a 
version of your program that does what you want.  Basically, you forgo the
templating a function over an integral type and instead create an overloaded
function that is overloaded based on a templated parameter.  The DimBase 
struct is needed, Dim does not have to derive off anything.  But it is useful
to have if you have an algorithm that will work in any dimension (overload the
the function to take just a DimBase) and specific versions for 1D, 2D, 3D
(overloaded on Dim<1>, Dim<2>, Dim<3>).



// foo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

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;
    }

}


int main()
{

  for( unsigned int j = 1; j <= 4; j++ )
    {
    std::cout << "Following should be " << j << std::endl;
    function(j);
    }
  return 0;
}


-----Original Message-----
From: Lydia Ng [mailto:lng@insightful.com]
Sent: Wednesday, October 17, 2001 8:31 PM
To: Insight-developers (E-mail)
Subject: [Insight-developers] help with template function problem


Hi,

I am trying to interface between template world and non-template world.
I have a problem with VC++ not being able to resolve
a "unsigned int" template parameter. See code and results below.
 
Does anyone know a fix/workaround to this for VC++?

I also ran this test on Linux/gcc-2.96 
which doesn't have this problem.

- Lydia


=========Code=================================
#include <iostream>

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

void function( unsigned int num )
{

  switch (num)
    {
    case 1:
      tfunction<1>();
      break;
    case 2:
      tfunction<2>();
      break;
    case 3:
      tfunction<3>();
      break;
    case 4:
      tfunction<4>();
      break;
    }

}


int main()
{

  for( unsigned int j = 1; j <= 4; j++ )
    {
    std::cout << "Following should be " << j << std::endl;
    function(j);
    }
  return 0;
}

===============VC++ results==========================
Following should be 1
4
Following should be 2
4
Following should be 3
4
Following should be 4
4

==============Linux/gcc 2.96 results=================
Following should be 1
1
Following should be 2
2
Following should be 3
3
Following should be 4
4

_______________________________________________
Insight-developers mailing list
Insight-developers@public.kitware.com
http://public.kitware.com/mailman/listinfo/insight-developers
_______________________________________________
Insight-developers mailing list
Insight-developers@public.kitware.com
http://public.kitware.com/mailman/listinfo/insight-developers