It is currently Fri Apr 28, 2017 11:03 am

All times are UTC - 5 hours




 Page 1 of 1 [ 3 posts ] 
Author Message
 Post subject: Working with a function template.
PostPosted: Wed Mar 25, 2015 5:56 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
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.


Offline
 Profile  
 
 Post subject: Re: Working with a function template.
PostPosted: Thu Mar 26, 2015 7:40 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
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]


Offline
 Profile  
 
 Post subject: Re: Working with a function template.
PostPosted: Thu Mar 26, 2015 8:28 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
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.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron