473,395 Members | 1,797 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,395 software developers and data experts.

Template question

Hi,
I have a function which looks like this:
template <typename Type1, typename Type2>
myfunc(void *dst, void *src);

Two variables:
int datatype1, int datatype2
datatype1 and datatype2 are coding the real type of dst and src (basic
integer types: unsigned char, int ...).
Template parameters Type and Type2 are determined by these 2 variables.

I need to call myfunc with potentially all combinations of datatype1 and
datatype2:
template <typename Type1, typename Type2>
myfunc<unsigned char, unsigned int>(void *dst, void *src);
myfunc<unsigned char, int>(void *dst, void *src);
....

A brute force solution is:
switch(datatype1) {
case UNSIGNED_CHAR:
switch(datatype2) {
case INT:
myfunc<unsigned char, int>(dst, src);
break;
...
}
...
}

I would like to avoid the use of a macro. Does somebody see a clean
solution with templates?

Thanks for your help.
Apr 1 '07 #1
4 1274
jukoli wrote:
Hi,
I have a function which looks like this:
template <typename Type1, typename Type2>
myfunc(void *dst, void *src);
Why are these void* rather than Type1* and Type2*?
--
Ian Collins.
Apr 1 '07 #2
On Apr 1, 4:19 pm, jukoli <j...@iiop.invalidwrote:
Hi,
I have a function which looks like this:
template <typename Type1, typename Type2>
myfunc(void *dst, void *src); // missing return type
Why prevent a compiler from doing its job?
Using void as type-placement is dead logic and bug-ridden.

Let the compiler help you code:

template< typename D, typename S >
void myfunc( D& dst, S& src )
{
// do stuff
}

where types D and S can be anything you require, including dumb
pointers.

#include<string>

int main()
{
int n(99);
std::string s("a short string");
myfunc< int, std::string >(n, s);
}

Apr 2 '07 #3
Salt_Peter a écrit :
On Apr 1, 4:19 pm, jukoli <j...@iiop.invalidwrote:
>Hi,
I have a function which looks like this:
template <typename Type1, typename Type2>
myfunc(void *dst, void *src); // missing return type

Why prevent a compiler from doing its job?
Using void as type-placement is dead logic and bug-ridden.
Because there is a setData(void *data, int type) somewhere else and I
can't remove it.
Apr 2 '07 #4
On Apr 2, 3:19 pm, jukoli <j...@iiop.invalidwrote:
Salt_Peter a écrit :On Apr 1, 4:19 pm, jukoli <j...@iiop.invalidwrote:
Hi,
I have a function which looks like this:
template <typename Type1, typename Type2>
myfunc(void *dst, void *src); // missing return type
Why prevent a compiler from doing its job?
Using void as type-placement is dead logic and bug-ridden.

Because there is a setData(void *data, int type) somewhere else and I
can't remove it.

Who says you need to?
Cast the appropriate parameter to void* when calling setData(...).

Apr 2 '07 #5

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

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
1
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.