Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Partially specialize a function template

Reply
Thread Tools

Partially specialize a function template

 
 
mathieu
Guest
Posts: n/a
 
      05-15-2008
Hi there,

I know this is not possible in c++. So my question, how should I
rewrite the following piece of code (without using a dummy class which
template parameter could be use for partial specialization).

template <typename TOut, typename TIn>
void InverseRescaleFunction(TOut *out, const TIn *in, double
intercept, double slope, size_t size)
{ ... }
template <typename TOut>
void InverseRescaleFunction(TOut *out, const float *in, double
intercept, double slope, size_t size)
{ ... }



Thanks !
 
Reply With Quote
 
 
 
 
mathieu
Guest
Posts: n/a
 
      05-15-2008
On May 15, 12:48 pm, mathieu <mathieu.malate...@gmail.com> wrote:
> Hi there,
>
> I know this is not possible in c++. So my question, how should I
> rewrite the following piece of code (without using a dummy class which
> template parameter could be use for partial specialization).
>
> template <typename TOut, typename TIn>
> void InverseRescaleFunction(TOut *out, const TIn *in, double
> intercept, double slope, size_t size)
> { ... }
> template <typename TOut>
> void InverseRescaleFunction(TOut *out, const float *in, double
> intercept, double slope, size_t size)
> { ... }
>
> Thanks !


Ok found a solution:

http://www.gotw.ca/publications/mill17.htm

....
// Example 4: Illustrating Moral #2
//
template<class T>
struct FImpl;

template<class T>
void f( T t ) { FImpl<T>::f( t ); } // users, don't touch this!

template<class T>
struct FImpl
{
static void f( T t ); // users, go ahead and specialize this
};
....

Sorry for the noise
-M
 
Reply With Quote
 
 
 
 
mathieu
Guest
Posts: n/a
 
      05-15-2008
On May 15, 1:21 pm, mathieu <mathieu.malate...@gmail.com> wrote:
> On May 15, 12:48 pm, mathieu <mathieu.malate...@gmail.com> wrote:
>
>
>
> > Hi there,

>
> > I know this is not possible in c++. So my question, how should I
> > rewrite the following piece of code (without using a dummy class which
> > template parameter could be use for partial specialization).

>
> > template <typename TOut, typename TIn>
> > void InverseRescaleFunction(TOut *out, const TIn *in, double
> > intercept, double slope, size_t size)
> > { ... }
> > template <typename TOut>
> > void InverseRescaleFunction(TOut *out, const float *in, double
> > intercept, double slope, size_t size)
> > { ... }

>
> > Thanks !

>
> Ok found a solution:
>
> http://www.gotw.ca/publications/mill17.htm
>
> ...
> // Example 4: Illustrating Moral #2
> //
> template<class T>
> struct FImpl;
>
> template<class T>
> void f( T t ) { FImpl<T>::f( t ); } // users, don't touch this!
>
> template<class T>
> struct FImpl
> {
> static void f( T t ); // users, go ahead and specialize this};
>
> ...
>
> Sorry for the noise
> -M


For reference:

template<typename TOut, typename TIn>
struct FImpl;

template<typename TOut, typename TIn>
void InverseRescaleFunction(TOut *out, const TIn *in, double
intercept, double slope, size_t size)
{ FImpl<TOut,TIn>::InverseRescaleFunction(out,in,int ercept,slope,size); } //
users, don't touch this!

template<typename TOut, typename TIn>
struct FImpl
{
static void InverseRescaleFunction( TOut *out, const TIn *in,
double intercept, double slope, size_t size) // users, go ahead
and specialize this
{
....
}
};

template<typename TOut>
struct FImpl<TOut, float>
{
static void InverseRescaleFunction(TOut *out, const float *in,
double intercept, double slope, size_t size)
{
....
}
};

-M
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      05-15-2008
On May 15, 3:48*am, mathieu <mathieu.malate...@gmail.com> wrote:
>
> * I know this is not possible in c++. So my question, how should I
> rewrite the following piece of code (without using a dummy class which
> template parameter could be use for partial specialization).
>
> template <typename TOut, typename TIn>
> void InverseRescaleFunction(TOut *out, const TIn *in, double
> intercept, double slope, size_t size)
> { ... }
> template <typename TOut>
> void InverseRescaleFunction(TOut *out, const float *in, double
> intercept, double slope, size_t size)
> { ... }


I'm wondering how you happen to know that the above pair of function
declarations is not possible in C++? Did you try using them in a C++
program? For example:

#include <iostream>

template <typename TOut, typename TIn>
void InverseRescaleFunction(TOut *out, const TIn *in, double
intercept, double slope, size_t size)
{
std::cout << "InverseRescaleFunction<TOut, TIn>\n";
}

template <typename TOut>
void InverseRescaleFunction(TOut *out, const float *in, double
intercept, double slope, size_t size)
{
std::cout << "InverseRescaleFunction<TOut>\n";
}

int main()
{
float f = 139.4;
int i;
long o;

InverseRescaleFunction(&i, &o, 2.2, 3.3, 4);
InverseRescaleFunction(&i, &f, 2.2, 3.3, 4);
}

Did your version of the above program fail to compile or did it
compile, run and produce the expected output shown below - as it did
on my machine?

Program Output:

InverseRescaleFunction<TOut, TIn>
InverseRescaleFunction<TOut>

Greg

 
Reply With Quote
 
mathieu
Guest
Posts: n/a
 
      05-15-2008
On May 15, 1:32 pm, Greg Herlihy <gre...@mac.com> wrote:
> On May 15, 3:48 am, mathieu <mathieu.malate...@gmail.com> wrote:
>
>
>
> > I know this is not possible in c++. So my question, how should I
> > rewrite the following piece of code (without using a dummy class which
> > template parameter could be use for partial specialization).

>
> > template <typename TOut, typename TIn>
> > void InverseRescaleFunction(TOut *out, const TIn *in, double
> > intercept, double slope, size_t size)
> > { ... }
> > template <typename TOut>
> > void InverseRescaleFunction(TOut *out, const float *in, double
> > intercept, double slope, size_t size)
> > { ... }

>
> I'm wondering how you happen to know that the above pair of function
> declarations is not possible in C++? Did you try using them in a C++
> program? For example:
>
> #include <iostream>
>
> template <typename TOut, typename TIn>
> void InverseRescaleFunction(TOut *out, const TIn *in, double
> intercept, double slope, size_t size)
> {
> std::cout << "InverseRescaleFunction<TOut, TIn>\n";
> }
>
> template <typename TOut>
> void InverseRescaleFunction(TOut *out, const float *in, double
> intercept, double slope, size_t size)
> {
> std::cout << "InverseRescaleFunction<TOut>\n";
> }
>
> int main()
> {
> float f = 139.4;
> int i;
> long o;
>
> InverseRescaleFunction(&i, &o, 2.2, 3.3, 4);
> InverseRescaleFunction(&i, &f, 2.2, 3.3, 4);
> }
>
> Did your version of the above program fail to compile or did it
> compile, run and produce the expected output shown below - as it did
> on my machine?
>
> Program Output:
>
> InverseRescaleFunction<TOut, TIn>
> InverseRescaleFunction<TOut>
>


Hi Greg

Indeed I tried your example and it actually produce the expected
results. However my full code, available at:

http://gdcm.svn.sourceforge.net/view...r.cxx?view=log

Did not work as expected. The only difference is that there was an
extra level of indirection. I'll work on your example to try to
reproduce the issue I was having with version 3163:

http://gdcm.svn.sourceforge.net/view...63&view=markup

Thanks
-Mathieu



 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      05-15-2008
On May 15, 5:22*am, mathieu <mathieu.malate...@gmail.com> wrote:
> On May 15, 1:32 pm, Greg Herlihy <gre...@mac.com> wrote:
>
>
>
> > On May 15, 3:48 am, mathieu <mathieu.malate...@gmail.com> wrote:

>
> > > * I know this is not possible in c++. So my question, how should I
> > > rewrite the following piece of code (without using a dummy class which
> > > template parameter could be use for partial specialization).

>
> > > template <typename TOut, typename TIn>
> > > void InverseRescaleFunction(TOut *out, const TIn *in, double
> > > intercept, double slope, size_t size)
> > > { ... }
> > > template <typename TOut>
> > > void InverseRescaleFunction(TOut *out, const float *in, double
> > > intercept, double slope, size_t size)
> > > { ... }

>
> > I'm wondering how you happen to know that the above pair of function
> > declarations is not possible in C++? Did you try using them in a C++
> > program? For example:

>
> > * * #include <iostream>

>
> > * * template <typename TOut, typename TIn>
> > * * void InverseRescaleFunction(TOut *out, const TIn *in, double
> > * * intercept, double slope, size_t size)
> > * * {
> > * * * * std::cout << "InverseRescaleFunction<TOut, TIn>\n";
> > * * }

>
> > * * template <typename TOut>
> > * * void InverseRescaleFunction(TOut *out, const float *in, double
> > * * intercept, double slope, size_t size)
> > * * {
> > * * * * std::cout << "InverseRescaleFunction<TOut>\n";
> > * * }

>
> > * * int main()
> > * * {
> > * * * * float f = 139.4;
> > * * * * int i;
> > * * * * long o;

>
> > * * * * InverseRescaleFunction(&i, &o, 2.2, 3.3, 4);
> > * * * * InverseRescaleFunction(&i, &f, 2.2, 3.3, 4);
> > * * }

>
> > Did your version of the above program fail to compile or did it
> > compile, run and produce the expected output shown below - as it did
> > on my machine?

>
> > * * Program Output:

>
> > * * InverseRescaleFunction<TOut, TIn>
> > * * InverseRescaleFunction<TOut>

>
> Hi Greg
>
> * Indeed I tried your example and it actually produce the expected
> results. However my full code, available at:
>
> http://gdcm.svn.sourceforge.net/view...e/MediaStorage...
>
> * Did not work as expected. The only difference is that there was an
> extra level of indirection. I'll work on your example to try to
> reproduce the issue I was having with version 3163:
>
> http://gdcm.svn.sourceforge.net/view...e/MediaStorage...


It is true that a function template cannot be partially specialized in
C++. But there is little reason why a C++ program would ever need to.
A C++ program can always overload a function with more than one
function template (just like the two function templates in your
original example overload the "InverseRescaleFunction" function). And
whenever multiple function templates overload the same function, the C+
+ compiler applies the rules of "partial ordering" to select the best
function template to match a particular function call.

So I do not think any particularly clever techniques are needed in
this situation. Simply declare the function templates that are needed
- and let the C++ compiler pick the best one to call.

Greg

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any way to partially specialize on a class defining a specifictype Pavel C++ 2 08-22-2010 06:35 PM
how to partially specialize a template class nested inside anothertemplate class? huili80@gmail.com C++ 5 06-22-2008 01:47 PM
specialize a template function that contains a template parameter sebastian C++ 1 09-17-2005 04:18 PM
How to partially specialize a class but NOT specialize a member function: mrstephengross C++ 1 08-02-2005 07:44 PM
Partially specialize a template with another template class Old Wolf C++ 4 04-08-2005 03:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57