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

G

Guest

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(typename 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
 
J

John Harrison

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(typename 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
 
G

Guest

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
 
J

John Harrison

[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
 
G

Guest

Thanks John,

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top