472,353 Members | 1,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Template functions and overloading

I am frustrated as I am trying to design a toy image library. I want
the user to write

mul(im, 2)

if s/he wants to multiply the image by 2, and

mul(im1, im2)

if s/he wants to multiply two images together. I would have like to
have two overloaded functions, the problem is that to be generic both
functions needs to have both arguments templated. Now I end up with
two functions having the same declaration, and that's bad.

Any idea to sort this mess out? I guess I have to have some mechanism
that could differentiate an image (by adding some special field or
typedef or inheritance from some class) from any other type during
precompilation, and do the if/else manually inside a single function.
But I am not sure how.

Thanks for you help,

B.
Jul 22 '05 #1
5 1405

"Bolin" <ga*******@voila.fr> schrieb im Newsbeitrag
news:93**************************@posting.google.c om...
I am frustrated as I am trying to design a toy image library. I want
the user to write

mul(im, 2)

if s/he wants to multiply the image by 2, and

mul(im1, im2)

if s/he wants to multiply two images together. I would have like to
have two overloaded functions, the problem is that to be generic both
functions needs to have both arguments templated. Now I end up with
two functions having the same declaration, and that's bad.

Any idea to sort this mess out? I guess I have to have some mechanism
that could differentiate an image (by adding some special field or
typedef or inheritance from some class) from any other type during
precompilation, and do the if/else manually inside a single function.
But I am not sure how.

If your class is a class template, where it is not allowed to specialize
member templates without specializing the class as well, I'd suggest to use
overloading, here is a small sample (using Andrei Alexandrescu's Loki):

<snip>
template<class T>
class CFoo{
// List all types you want to use with the numeric implementation.
typedef TYPELIST_4(int, unsigned int, float, double) NumericTypes_t;
public:
// actual mul template function
template<class T1, class T2>
T1 mul(T1 p1, T2 p2){
// switches by a dummy parameter between a numeric and a not
numeric implementation.
return mulSwitchByOlverload(p1,p2, Loki::Int2Type<
Loki::TL::IndexOf<NumericTypes_t, T2>::value >= 0>());
}
private:
template<class T1, class T2>
T1 mulSwitchByOlverload(T1 pa1, T2 p2, Loki::Int2Type<true>){
printf("Mult by numeric...\n");
return T1();
}

template<class T1, class T2>
T1 mulSwitchByOlverload(T1 pa1, T2 p2, Loki::Int2Type<false>){
printf("Mult not numeric...\n");
return T1();
}
</snip>
Jul 22 '05 #2
On 17 Oct 2004 18:10:58 -0700, ga*******@voila.fr (Bolin) wrote:
I am frustrated as I am trying to design a toy image library. I want
the user to write

mul(im, 2)

if s/he wants to multiply the image by 2, and

mul(im1, im2)

if s/he wants to multiply two images together. I would have like to
have two overloaded functions, the problem is that to be generic both
functions needs to have both arguments templated. Now I end up with
two functions having the same declaration, and that's bad.

Any idea to sort this mess out? I guess I have to have some mechanism
that could differentiate an image (by adding some special field or
typedef or inheritance from some class) from any other type during
precompilation, and do the if/else manually inside a single function.
But I am not sure how.


If you download and install boost, you can do something like this:

template <typename T1, typename T2>
typename boost::enable_if<boost::is_numeric<T2>, T1>::type
mul(T1 const& t1, T2 t2)
{
//implement numeric version
}

template <typename T1, typename T2>
typename boost::enable_if_c<!boost::is_numeric<T2>::value, T1>::type
mul(T1 const& t1, T2 const& t2)
{
//implement image version
}

I think you'll need an up-to-date compiler to handle it though.

Tom
Jul 22 '05 #3
Thanks for both your answers. The problem is that I cannot assume that
'non-image' types are numeric types. For example, I might have an
image of vectors, and the user could write

mul(vecim, scalar);
mul(vecim, vector); // say, element-wise multiplication
mul(vecim, matrix);
....

and all would call the 'non-image' version of mul. Actually this 'mul'
is expected to work whenever the self-multiplication of element of the
image with the second parameter is possible.
Tom Widmer <to********@hotmail.com> wrote in message news:<5u********************************@4ax.com>. ..
On 17 Oct 2004 18:10:58 -0700, ga*******@voila.fr (Bolin) wrote:
If you download and install boost, you can do something like this:

template <typename T1, typename T2>
typename boost::enable_if<boost::is_numeric<T2>, T1>::type
mul(T1 const& t1, T2 t2)
{
//implement numeric version
}

template <typename T1, typename T2>
typename boost::enable_if_c<!boost::is_numeric<T2>::value, T1>::type
mul(T1 const& t1, T2 const& t2)
{
//implement image version
}

I think you'll need an up-to-date compiler to handle it though.

Tom

Jul 22 '05 #4
On 18 Oct 2004 07:23:19 -0700, ga*******@voila.fr (Bolin) wrote:
Thanks for both your answers. The problem is that I cannot assume that
'non-image' types are numeric types. For example, I might have an
image of vectors, and the user could write

mul(vecim, scalar);
mul(vecim, vector); // say, element-wise multiplication
mul(vecim, matrix);
...

and all would call the 'non-image' version of mul. Actually this 'mul'
is expected to work whenever the self-multiplication of element of the
image with the second parameter is possible.


You'll need some kind of traits class to dispatch to the correct
implementation - I assume you'll have 3 implementations of mul, to go
with the three cases above.

template <class T>
struct dimension_traits
{
static int const dimensions =
dimension_traits_helper<T, is_scalar<T>::value,
is_container<T>::value>::dimensions;
};

or something like that. Then you can use the enable_if technique, or
ordinary overloading. Alternatively you could redesign your interface
a bit, perhaps to use iterators for the vector case.

Tom
Jul 22 '05 #5
Tom Widmer <to********@hotmail.com> wrote in message news:<tb********************************@4ax.com>. ..
template <class Image, class T>
void mul(Image const& im, T const& t)
{
//dispatch depending on whether T is an image or not.
mul(im, t, int_to_type<image_traits<T>::is_image>());
}
Thanks, that's what was needed. Works much better than my if/then.

That's the standard "old-style" way that dispatch is done using
overloading. The new way is to use enable_if.


Thanks for the tip.

B.
Jul 22 '05 #6

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

Similar topics

9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func();...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't...
6
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does...
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,...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.