2

Is there a way to test a parameter's data type in an if statement? Here is an example of source code: it will not compile but it used to present my intentions.

typedef char cInt[sizeof(int)];
typedef char cFloat[sizeof(float)];
typedef char cDouble[sizeof(double)];

template<typename T>
char* convertToCharStr( const T& t ) {
    if ( t == int ) {
        cInt c = t;
        return c;
     }
     if ( t == float ) {
        cFloat c = t;
        return c;
     }
     if ( t == double ) {
        cDouble c = t;
        return c;
     }
     return nullptr;
}

As you can see, I am trying to create a template function that can take any default data types such as an int, unsigned int, float, double and depending on the data type it will create the appropriate char[] variable on the stack according to the data type passed in and store the data into the char[] and return the pointer out of the function.

I will leave this above the way it is, but as a note the character arrays should of been unsigned char.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • 3
    http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – chris Mar 25 '15 at 02:18
  • 1
    Memory for `int`s, `float`s and `double`s has alignment requirements that your code ignores. Secondly, when your function returns the stack memory for its variables is automatically released and available for future function calls and other uses. Thirdly, you should prefer overloading or template specialisation instead of run-time switching inside a template. Fourth: what you're trying to do is slightly better done by returning `sizeof t` and letting the caller reserve corresponding space on *their* stack, or using the heap, and it'd be hugely better to use `boost::variant`. – Tony Delroy Mar 25 '15 at 02:59

1 Answers1

7

Well, there are ways using typeid as done here Using typeid to check for template type , but I would suggest using template specialization instead, like so:

template<typename T>
char* convertToCharStr( const T& t ) {
    //Maybe you might feel it more appropriate to put a static assert here 
    return nullptr; 
}

template<>
char* convertToCharStr( const int& t ) {
    cInt c = t;
    return c;
}

template<>
char* convertToCharStr( const float& t ) {
    cFloat c = t;
    return c;
}

template<>
char* convertToCharStr( const double& t ) {
    cDouble c = t;
    return c;
}

See CPP Reference and this question for more of a discusion on it (and what else you can do..to avoid the pains of specialized templating). C++ templates specialization syntax

That being said, creating a variable on the stack and then returning a pointer to it is unsafe, as that variable will be unstacked on return of the function call and will likely be overwritten later on.

Community
  • 1
  • 1
Rollen
  • 1,212
  • 11
  • 15
  • Yeah after I posted this question and looked at the code again, I realized it would be better to pass in a second param an unsigned char* by reference as opposed to return a stack char* – Francis Cugler Mar 25 '15 at 06:31
  • That would be more likely to work out as long as you allocate on the heap. – Rollen Mar 25 '15 at 20:15
  • some one else mentioned to me that I could just use a union of the data type such as int or float with an unsigned char pointer, but my goal was to assign the individual bytes of that type into each unsigned char array index. – Francis Cugler Mar 27 '15 at 07:54