Partially specialize a function template

M

mathieu

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 !
 
M

mathieu

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
 
M

mathieu

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,intercept,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
 
G

Greg Herlihy

  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
 
M

mathieu

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/vie...torageAndFileFormat/gdcmRescaler.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/vie...at/gdcmRescaler.cxx?revision=3163&view=markup

Thanks
-Mathieu
 
G

Greg Herlihy

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/viewvc/gdcm/trunk/Source/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/viewvc/gdcm/trunk/Source/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top