MarekKnows Discussion Forum
http://marekknows.com/phpBB3/

Working with a function template.
http://marekknows.com/phpBB3/viewtopic.php?f=6&t=850
Page 1 of 1

Author:  skilz80 [ Wed Mar 25, 2015 5:56 pm ]
Post subject:  Working with a function template.

I would like to create a function template that accepts an arbitrary value of any of the default data types, short, int, float, double, long etc. Then depending on the type passed in to the function I would like to save the byte components in to the appropriate char[] and pass out of the function a pointer to that array. Here is some pseudo code to give an idea of what my intentions are.

typedef unsigned char cShort[sizeof(short)];    // char[2]
typedef unsigned char cInt[sizeof(int)];        // char[4]
typedef unsigned char cFloat[sizeof(float)];    // char[4]
typedef unsigned char cDouble[sizeof(double)];  // char[8]

template<typename T>
void convertToCharStr( const T& t, unsigned char*& pChar ) {
    // Pending On The Data Type (t), use the appropriate typedef above
    // Assign the first byte into char[0], 2nd into [1] etc.
    // save the stack variable typedef defined above into the unsigned char* passed in by reference.
}


This should be non trivial yet, it appears to be a little more complicated or complex then it first appears.
The reason this isn't so simple is due to the fact there is no easy way of doing this in c++
 
      // Inside some function used by template type with type t passed in
     if ( t == int ) {
       // do this
    }
    if ( t == float ) {
       // do this
    }
    // ... and so on.


This is where the complication comes in. This is only half the problem but once I can get something like this to work correctly the opposite should be easy to do; converting the character* back to any other data type.

Any suggestions or help would be very helpful.

Author:  Marek [ Thu Mar 26, 2015 7:40 am ]
Post subject:  Re: Working with a function template.

skilz80 wrote:
I would like to create a function template that accepts an arbitrary value of any of the default data types, short, int, float, double, long etc. Then depending on the type passed in to the function I would like to save the byte components in to the appropriate char[] and pass out of the function a pointer to that array.


My first question is why? Why do you want to do it this way with a template?

You could very easily do this by defining a union between all the different types you want to support, and then just return the &charUnionValue[0]

Author:  skilz80 [ Thu Mar 26, 2015 8:28 am ]
Post subject:  Re: Working with a function template.

Just to see how it could be done, templates are my weakness in C++ an the only way to get better at creating them is by practice. Just thought it would of been a good challenge. But I can see how the union with a data type and an unsigned char pointer would work.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/