473,397 Members | 2,056 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,397 software developers and data experts.

Template where I need to know the type

Hello!

In my code I have functions like

SendToNode(int dest_node; int i);
SendToNode(int dest_node; double d);
SendToNode(int dest_node; unsigned int u);

and so on, where the function body is the same for all
these overloaded functions, except for one line, which
depends on the type of the second argument to SendToNode,
i.e. this line looks (more or less) like

internal_send_function(dest_node, type_is_int);
internal_send_function(dest_node, type_is_double);
internal_send_function(dest_node, type_is_unsigned_int);

where "type_is_int" etc. are symbolic constants of type int.

The situation is I'm not much of an OOP programmer, still
it looks to me like my SendToNode function is a candidate for
a template. The problem is: how do I do the "branching" in the
template, depending on what the *type* of the argument is?
How do I determine the type of that argument? I tried googling
it up and found something called typeof(), but it looked as if
it was not a part of the standard (perhaps "yet").

Is this where the concept of traits comes into play? What's
the story behind this?

TIA,
- J.
Jul 19 '05 #1
3 2112
Jacek Dziedzic wrote:
Hello!

In my code I have functions like

SendToNode(int dest_node; int i);
SendToNode(int dest_node; double d);
SendToNode(int dest_node; unsigned int u);

and so on, where the function body is the same for all
these overloaded functions, except for one line, which
depends on the type of the second argument to SendToNode,
i.e. this line looks (more or less) like

internal_send_function(dest_node, type_is_int);
internal_send_function(dest_node, type_is_double);
internal_send_function(dest_node, type_is_unsigned_int);

where "type_is_int" etc. are symbolic constants of type int.

The situation is I'm not much of an OOP programmer,
Lucky for you, templates don't have much to do with OOD.
still
it looks to me like my SendToNode function is a candidate for
a template. The problem is: how do I do the "branching" in the
template, depending on what the *type* of the argument is?
It's called partial specialization, I think. But it won't help you much
here.
How do I determine the type of that argument? I tried googling
it up and found something called typeof(),
typeof() is a gcc extension, I think.
but it looked as if
it was not a part of the standard (perhaps "yet").

Is this where the concept of traits comes into play? What's
the story behind this?


This seems pretty straightforward to me. Refactor your code into a
function that does the shared stuff, and then add the one line that
differs to each of the specialized functions. It appears that all you
want to do is pass an int that depends on the static type, so you can
use overloading.

enum eType { eUnknown, eInt, eUnsignedInt, eDouble };

void internal_send_function(int dest_node, enum eType type) {
/* stuff goes here */
}

void SendToNode(int dest_node, int i) {
internal_send_function(dest_node, eInt);
}

void SendToNode(int dest_node, unsigned int i) {
internal_send_function(dest_node, eUnsignedInt);
}

void SendToNode(int dest_node, double i) {
internal_send_function(dest_node, eDouble);
}

It's not clear from the code you showed, but it looks like you aren't
using the value of the passed in variable at all, which makes me
question what exactly you're doing.

-Peter

Jul 19 '05 #2
"NFish" <no****@nowhere.net> wrote in message
news:6y*****************@newssvr27.news.prodigy.co m...
[...]
This seems pretty straightforward to me. Refactor your code
into a function that does the shared stuff, and then add the one
line that
differs to each of the specialized functions. It appears that all you
want to do is pass an int that depends on the static type, so you
can
use overloading.

enum eType { eUnknown, eInt, eUnsignedInt, eDouble };

void internal_send_function(int dest_node, enum eType type) {
/* stuff goes here */
}

void SendToNode(int dest_node, int i) {
internal_send_function(dest_node, eInt);
}

void SendToNode(int dest_node, unsigned int i) {
internal_send_function(dest_node, eUnsignedInt);
}

void SendToNode(int dest_node, double i) {
internal_send_function(dest_node, eDouble);
}
Yes, but that still leaves me with a one-function-needed-
per-every-type mess. Even though these functions are now
reduced to one line, I thought that by some "template magic"
I could stuff all that together into one function that would
perhaps have some branches depending on the type of
the argument, but not individual extra functions for every
possible type that I'd use.
It's not clear from the code you showed, but it looks like you
aren't using the value of the passed in variable at all, which
makes me question what exactly you're doing.


Ah yes, that was only a simplification, but I indeed forgot
to include the variable itself in the argument list of
SendToNode...

- J.
Jul 19 '05 #3
In article <21**************@janowo.net>,
"Jacek Dziedzic" <ja***********@janowo.net> wrote:
Hello!

In my code I have functions like

SendToNode(int dest_node; int i);
SendToNode(int dest_node; double d);
SendToNode(int dest_node; unsigned int u);

and so on, where the function body is the same for all
these overloaded functions, except for one line, which
depends on the type of the second argument to SendToNode,
i.e. this line looks (more or less) like

internal_send_function(dest_node, type_is_int);
internal_send_function(dest_node, type_is_double);
internal_send_function(dest_node, type_is_unsigned_int);

where "type_is_int" etc. are symbolic constants of type int.

The situation is I'm not much of an OOP programmer, still
it looks to me like my SendToNode function is a candidate for
a template. The problem is: how do I do the "branching" in the
template, depending on what the *type* of the argument is?
How do I determine the type of that argument? I tried googling
it up and found something called typeof(), but it looked as if
it was not a part of the standard (perhaps "yet").

Is this where the concept of traits comes into play? What's
the story behind this?


Maybe something like:

template <class T>
void
internal_send_function(int dest_node, T);

template <>
void
internal_send_function(int dest_node, int i) {}

template <>
void
internal_send_function(int dest_node, double d) {}

template <>
void
internal_send_function(int dest_node, unsigned int u) {}

template <class T>
void
SendToNode(int dest_node, T t)
{
// common stuff
internal_send_function(dest_node, t);
// common stuff
}

-Howard
Jul 19 '05 #4

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

Similar topics

9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
2
by: James Brown | last post by:
Hi again, I'm referring back to my previous posting of the same title, with everyone's help I've now got a better understanding of what my goals are and I have a class which now looks like: ...
8
by: Thomas Heller | last post by:
I need to convert C preprocessor definitions into python code. The definitions are dumped out of gccxml (see http://www.gccxml.org) , running over the windows header files (for example). This...
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 overloaded by the return types?. Is this Ok: ? ...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
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...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.