473,800 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1478

"Bolin" <ga*******@voil a.fr> schrieb im Newsbeitrag
news:93******** *************** ***@posting.goo gle.com...
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 mulSwitchByOlve rload(p1,p2, Loki::Int2Type<
Loki::TL::Index Of<NumericTypes _t, T2>::value >= 0>());
}
private:
template<class T1, class T2>
T1 mulSwitchByOlve rload(T1 pa1, T2 p2, Loki::Int2Type< true>){
printf("Mult by numeric...\n");
return T1();
}

template<class T1, class T2>
T1 mulSwitchByOlve rload(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_i f<boost::is_num eric<T2>, T1>::type
mul(T1 const& t1, T2 t2)
{
//implement numeric version
}

template <typename T1, typename T2>
typename boost::enable_i f_c<!boost::is_ numeric<T2>::va lue, 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********@hot mail.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_i f<boost::is_num eric<T2>, T1>::type
mul(T1 const& t1, T2 t2)
{
//implement numeric version
}

template <typename T1, typename T2>
typename boost::enable_i f_c<!boost::is_ numeric<T2>::va lue, 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_trait s
{
static int const dimensions =
dimension_trait s_helper<T, is_scalar<T>::v alue,
is_container<T> ::value>::dimen sions;
};

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********@hot mail.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<ima ge_traits<T>::i s_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
2320
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 different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
2
2476
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 output stream. if i dont override it, it works fine. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include <string> #include <vector>
5
6590
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(); }; template<class T, unsigned N>
11
2492
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 class 'C', but only because a trait tell us so: class A {}; template <typename T> struct Is_C { static const bool value = false;
9
2733
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 overloaded by the return types?. Is this Ok: ? template <class T1, class T2> int getValue( T1 col, T2 row ) ; template <class T1, class T2> double getValue( T1 col, T2 row ) ;
22
6033
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 see a reason why a template function can't be given extern "C" linkage where it is to be assigned to a C function pointer. Ian
6
5017
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 change the number of parameters, but otherwise they seem very similar to me - i.e. they both provide specialized functions for depending on the parameter types. Thanks
272
14203
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 anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
8
2438
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:
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.