Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / February 2004



Tip: Looking for answers? Try searching our database.

Recursive algorithm using partially specialized function template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
iTris - 30 Jan 2004 02:51 GMT
Hi,

I'm trying to get CodeWarrior 9.1 to compile the code like the one
above. When I compile it, the compiler just hangs after having parsed
every lines. By chance The build window "stop" button still works. I've
simplified it but my real case generates the same problem. To me, the
compiler should deduce func<double,2> and then func<double,1> from the
general template, then func<double,0> from the specialization and stop.
 Having the "ISO C++ template parser" option switched on or off doesn't
after the problem.  I didn't manage to make xcode compile the same code
without errors. I hope it's just some nasty syntax trick, otherwise I
must have to rewrite a lot of my code. Hope someone have a clue.

Thanks a lot

Tristan

here's my code :

#include<iostream>

template<typename tType,int tNb>
tType func(tType value)
{
    return func<double,tNb-1>(value-1.0);
}

template<>
template<typename tType>
tType func<tType,0>(tType value)
{
    return func(value-1.0);
}

int main()
{
    std::cout<<func<double,2>(10.0)<<"should be 8.0"<<std::endl;
    return 0;
}
Miro Jurisic - 30 Jan 2004 04:50 GMT
There is no such thing as partially specialized function template. You can only
partially specialize class templates. For example:

#include<iostream>

template<typename T,int N>
struct funcimpl{
static T impl(T val)
{
    return funcimpl<double,N-1>::impl(val-1.0);
}
};

template<typename T>
struct funcimpl<T, 0>{
static T impl(T val)
{
    return val;
}
};

template <typename T, int N>
T func(T val)
{
  return funcimpl<T, N>::impl(val);
}

int main()
{
    std::cout<<func<double,2>(10.0)<<"should be 8.0"<<std::endl;
    return 0;
}

Signature

If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>

Derek Ledbetter - 30 Jan 2004 11:16 GMT
> I'm trying to get CodeWarrior 9.1 to compile the code like the one
> above. When I compile it, the compiler just hangs after having parsed
[quoted text clipped - 6 lines]
> without errors. I hope it's just some nasty syntax trick, otherwise I
> must have to rewrite a lot of my code. Hope someone have a clue.

In C++, only class templates can be speciallized, not function
templates.  Here's one way to do it:

   template<typename tType,int tNb>
   tType func(tType value)
   {
       if(tNb != 0)
       {
           return func<tType,tNb-1>(value-1.0);
       }
       else
       {
           return value-1.0;
       }
   }

Here's another:

   template<typename tType,int tNb>
   struct funcStruct
   {
       static tType func(tType value)
       {
           return funcStruct<tType,tNb-1>::func(value-1.0);
       }
   };
   
   template<typename tType>
   struct funcStruct<tType,0>
   {
       static tType func(tType value)
       {
           return value-1.0;
       }
   };

Signature

Derek Ledbetter
derekl@serve.com

Heavy boots of lead
fills his victims full of dread
Running as fast as they can
Iron Man lives again!

Derek Ledbetter - 07 Feb 2004 03:59 GMT
>     template<typename tType,int tNb>
>     struct funcStruct
[quoted text clipped - 13 lines]
>         }
>     };

I should have said that only class templates can be _partially_
specialized.  Function templates can be explicitly _fully_ specialized.
Also, my first example can't be instantiated, since func<tType, 0> will
instantiate func<tType, -1>, which will instantiate func<tType, -2>, and
so on.  These functions won't be called, but this infinite sequence of
instantiation will cause compilation to fail.  Here's a fix:

   template<typename tType,int tNb>
   tType func(tType value)
   {
       if(tNb != 0)
       {
           return func<tType, ((tNb != 0)?(tNb-1):0) >(value-1.0);
       }
       else
       {
           return value-1.0;
       }
   }

So func<tType, 0> contains a call to itself, but this call will not be
executed.

Signature

Derek Ledbetter
derekl@serve.com

Heavy boots of lead
fills his victims full of dread
Running as fast as they can
Iron Man lives again!

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.