I am using templates for my struct like:
#pragma pack(push, 1)
template <typename T>
struct S
{
T t;
inline void Set(const T& val) { t = val; }
}
#pragma pack(pop)
T can be an float, int, short or char[10], char[1] or char[2] (and preferably any length would be great).
While the above seems to work quite nicely for the integral types, I’m having difficulty implementing the char[n] portion in that:
- I need to use strncpy or memcpy instead of the assignment operator
- Using the above, the compiler complains about the signature (const char[2]& val) and my calling it via s.Set(“T”).
- The interface between S with integral and character types has to be same as it’s generic code that is calling them (and it doesn’t care what type they are).
You can define template specializations for
Tin the case ofchar[10], etc. Do any issues remain when you do that? But as Mat has already noted, using a string is an approach worth considering.http://codepad.org/X8YVuFja output: