[Insight-users] C++ question

Karthik Krishnan karthik.krishnan at kitware.com
Wed Sep 24 11:04:26 EDT 2008


On Wed, Sep 24, 2008 at 10:55 AM, Luke Bloy <luke.bloy at gmail.com> wrote:
> Hi All,
>
> I'm not, by a long shot, a c++ guru so i have perhaps a pretty basic
> question.
>
> I'd like to be able to do something like this...
>
> int main(int argc, char** argv)
> {
>    typedef double
>  PixelType;
>    unsigned int dimension = 4;
>
>    typedef itk::Image<PixelType, dimension>        ImageType;
> }
>
> This won't compile because dimension isn't const. Ideally i'd like this to
> be passed in as a command line argument. Is there a way to get around the
> typedef?

Nope. templates have to be instantiated at compile time, not at run time.

To get the behaviour you desire, what we tend to do is to instantiate
all possible templates and choose the right template at run-time,

something like

  switch (dimension)
    case 3:
      {
      typedef itk::Image<PixelType, 3> ImageType;
      // do stuff
      }
    case 2:
      {
      typedef itk::Image<PixelType, 2> ImageType;
      // do stuff
      }

>
> Thanks for any help.
> -Luke
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>



-- 
Karthik Krishnan
R&D Engineer,
Kitware Inc.
Ph: 518 371 3971 x119
Fax: 518 371 3971


More information about the Insight-users mailing list