473,835 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

umm... something... template(s)... something else... pointer(s)... and such... 0.o yah, I'm hopeless and clueless o.0

Ok... well... I'm a noob as far as templates go, and casting and such...
never bothered to learn much about this stuff... but now I've got a good
reason to care, so i'm just wondering what's going on in this code. I'm
assuming that T is *always* going to be some number (bool, INT16, int,
INT32, etc...) but no reason this shouldn't work on chars' and such... (...
*giggle* or pointers... could... work... on... pointers... don't make your
screen go pOp...)

template <class T> void SetValueOfT(typ ename T * &pT)
{
DWORD dwSetTo = 1;

pT = static_cast<T *>dwSetTo;
}

main(...)
{
int x = 0;

// x is 0
SetValueOfT(&x) ;
// now x is 1

bool y = false;

// y is false
SetValueOfT(&y) ;
// now y is true
}

In these cases... x will be 1, and y will be true... and that's great in a
"hey I want this to work today" kind of way... but I don't really understand
what's going on inside the SetValueOfT function. I'm guessing with x,
dwSetTo is being casted to an int before assigning the value to x... does
that mean that dwSetTo get's the value of 1, then a temporary variable is
created, of type (or at least sizeof) int, and given the value of 1, before
passing that value to x? Is there a way to prevent the copy from being made
(if it does get made...). I guess i could have made dwSetTo of type T to
speed things up, but then if I wanted to say pass dwSetTo to a few functions
before assinging it's value to pT, i'd have to cast alot more. Anyways... If
you feel like rambling, helping, tossing out links, complaining that I
didn't search hard enough (long as you don't start a full fledged flam ~.^)
please do, acause I'm hopeless and clueless right now. Also the more
detailed the better (talk ASM if yah want... I'd actually prefer to see
what's going on in ASM, since the code get's turned into that stuff anyways
^.~)

Ohh... one more thing before I forget... what's with the "typename T * &pT"
looks like for the x case it compresses to "int * &pT"... now I'd get it if
it was "int * pT" that's an int pointer... but I don't get the & before the
pT... I've seen it in a few headers, and havn't really got what it's all
about... My guess is it means pass the function a pointer to the variable,
but the function will pretend like it's playing with a variable... for
example:

// could also be...
// void blah(int* anInt)
void blah(int &anInt)
{
// could also be...
// (*anInt)++;
anInt++;
}

.... main (...)
{
int myInt = 5

blah(&myInt);
// myInt is now 6
}

These are all just (probably wrong 0.o) guesses though... so please correct
me and fill my head with your details ;)

Thanks in advance,
Dead RAM
Jul 22 '05 #1
4 1352

<de*****@hotmai l.com> wrote in message
news:Et******** ************@ro gers.com...
Ok... well... I'm a noob as far as templates go, and casting and such...
never bothered to learn much about this stuff... but now I've got a good
reason to care, so i'm just wondering what's going on in this code. I'm
assuming that T is *always* going to be some number (bool, INT16, int,
INT32, etc...) but no reason this shouldn't work on chars' and such...
(... *giggle* or pointers... could... work... on... pointers... don't make
your screen go pOp...)

template <class T> void SetValueOfT(typ ename T * &pT)
{
DWORD dwSetTo = 1;

pT = static_cast<T *>dwSetTo;
}

main(...)
{
int x = 0;

// x is 0
SetValueOfT(&x) ;
// now x is 1

bool y = false;

// y is false
SetValueOfT(&y) ;
// now y is true
}


I don't know where you got this code from but it has at least three errors
and even when these are fixed it is completely misconcieved and will not do
what your comments say it will do.

I'm guessing that what you actually want is the much simpler code

template <class T> void SetValueOfT(T * pT)
{
DWORD dwSetTo = 1;

*pT = static_cast<T>( dwSetTo);
}

int main(...)
{
int x = 0;

// x is 0
SetValueOfT(&x) ;
// now x is 1

bool y = false;

// y is false
SetValueOfT(&y) ;
// now y is true
}

Since the posted code was bogus, so are the speculations on it, so I'm
stopping here. If my code wasn't what you really intended then post some
code that actually compiles and we'll try again.

john
Jul 22 '05 #2

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2s******** *****@uni-berlin.de...

I'm guessing that what you actually want is the much simpler code

template <class T> void SetValueOfT(T * pT)
{
DWORD dwSetTo = 1;

*pT = static_cast<T>( dwSetTo);
}

int main(...)
{
int x = 0;

// x is 0
SetValueOfT(&x) ;
// now x is 1

bool y = false;

// y is false
SetValueOfT(&y) ;
// now y is true
}

Since the posted code was bogus, so are the speculations on it, so I'm
stopping here. If my code wasn't what you really intended then post some
code that actually compiles and we'll try again.


Sorry... wasn't thinking properly... very bogus...

The above code (your new version of my old mess) is the code I intended to
use. What's really important to me is what this template stuff ends up
doing... in any sort of general way... it's hard to get to abstract with
some of this stuff though ;) I guess I'm wondering if at compile time a
function for each type (of T) is generated, and what kind of slowdowns or
bulk and the sorts ends up in most peoples code. I've got all my templates
working (... now I do anyways...), but I want to understand what was going
on. I guessing, since this stuff could end up in a dll and the likes, that
there is only one function created, unless you specify the "template < >
SomeThing < aSpecialType > (...)". But all I really have is alot of
guesswork and code that I bruteforced from a verbose compiler 0.o
Jul 22 '05 #3
[snip]
What's really important to me is what this template stuff ends up
doing... in any sort of general way... it's hard to get to abstract with
some of this stuff though ;) I guess I'm wondering if at compile time a
function for each type (of T) is generated,
That's right.
and what kind of slowdowns or
bulk and the sorts ends up in most peoples code.
A compiler/linker should be able to remove multiple copies of generated
functions, ut this is a QOI issue of course.
I've got all my templates
working (... now I do anyways...), but I want to understand what was going
on. I guessing, since this stuff could end up in a dll and the likes, that
there is only one function created, unless you specify the "template < >
SomeThing < aSpecialType > (...)". But all I really have is alot of
guesswork and code that I bruteforced from a verbose compiler 0.o


It doesn't make sense to try an put a template in a DLL, since templates
cannot be object code, only instantiations of templates are object code.
Since templates don't get instantiated until they are used there is nothing
to put in a DLL. This is the same reason that template code goes in header
files not in source files.

You can explicitly instantiate a template and that instantiated template
could go in a DLL. But then you are limited to the instantiates you make
ahead of time (when you compile the DLL).

john
Jul 22 '05 #4
Thanks John,

Sorry for taking so long.. just got to coding... and forgot to get to
thanking ;) You're a great help ;)
Jul 22 '05 #5

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

Similar topics

12
4593
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the function (double, float etc) ?? Thanks in advance,
2
5971
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I get the following error: Error: Unresolved external 'operator <<(std::basic_ostream<char, std::char_traits<char> >&, const BinomialTree<int>&)' referenced from C:\DOCUMENTS AND SETTINGS\RYAN\DESKTOP\COMPUTER SCIENCE\CS
3
2736
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename Foo> class Bar { private: Foo foo;
22
6035
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
3
3763
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
45
2955
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 to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
1
3210
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some algorithms don't need to distinguish black/gray, but have an unused value in e.g. a distance map, e.g. -1, to which the map can be initialised. So I tried to write a map adapter which can be passed such a map together with the value indicating "white". This adapter will return
3
2347
by: Markus Dehmann | last post by:
I have an abstract base class which contains a function that I would like to template, but virtual template functions are illegal. I put a mini code example below, which doesn't do anything great, but reflects the structure of my problem. In a nutshell, the abstr. base class VectorCreator has a function createVector whose return type is unfortunately fixed to vector<double>. Only now I discovered that some implementations would like to...
6
391
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
9802
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
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
10804
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
10517
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...
1
10558
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9343
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
5631
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4430
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
3
3086
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.