473,416 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Partially specialize a function template

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 !
Jun 27 '08 #1
7 1898
On May 15, 12:48 pm, mathieu <mathieu.malate...@gmail.comwrote:
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
Jun 27 '08 #2
On May 15, 1:21 pm, mathieu <mathieu.malate...@gmail.comwrote:
On May 15, 12:48 pm, mathieu <mathieu.malate...@gmail.comwrote:
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
Jun 27 '08 #3
On May 15, 3:48*am, mathieu <mathieu.malate...@gmail.comwrote:
>
* 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

Jun 27 '08 #4
On May 15, 1:32 pm, Greg Herlihy <gre...@mac.comwrote:
On May 15, 3:48 am, mathieu <mathieu.malate...@gmail.comwrote:
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

Jun 27 '08 #5
On May 15, 7:48*am, mathieu <mathieu.malate...@gmail.comwrote:
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)
{ ... }

Hi Mathieu.

You're correct that partial specialization of functions is not allowed
in C++. However, in this case you have actually overloaded the
function - that's why it should work. An example of what would be a
partial specialization of your function (that is not allowed) would
look like this:

template <typename TOut>
void InverseRescaleFunction<float>(TOut *out, const float *in, double
intercept, double slope, size_t size)
{ ... }
--
Leandro T. C. Melo


Jun 27 '08 #6
On May 15, 9:34*am, ltcmelo <ltcm...@gmail.comwrote:
On May 15, 7:48*am, mathieu <mathieu.malate...@gmail.comwrote:
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)
{ ... }

Hi Mathieu.

You're correct that partial specialization of functions is not allowed
in C++. However, in this case you have actually overloaded the
function - that's why it should work. An example of what would be a
partial specialization of your function (that is not allowed) would
look like this:

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

Just a fix... it would look like this:

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

--
Leandro T. C. Melo
Jun 27 '08 #7
On May 15, 5:22*am, mathieu <mathieu.malate...@gmail.comwrote:
On May 15, 1:32 pm, Greg Herlihy <gre...@mac.comwrote:
On May 15, 3:48 am, mathieu <mathieu.malate...@gmail.comwrote:
* 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

Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Old Wolf | last post by:
I have a template class like this: template<typename T, int N> struct Foo { ........... }; which I have successfully specialized for some types, eg:
1
by: mrstephengross | last post by:
Ok, I've got a class with two template parameters (A and B), and a member function foo(). I want to specialize foo for a particular class A. Is this possible? The following code shows an example: ...
1
by: sebastian | last post by:
Hi, I'd like to specialize a template function that contains a template parameter. In Example i have the following function declared: .... template < int i > static stupid_object&...
3
by: PengYu.UT | last post by:
I have the following two program. The first one compiles well, but the second one doesn't. The only difference between them is one more template parameter is added for the second program. Would...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.