Sign in to follow this  
sfx81

c++ template how to

Recommended Posts

Hi All,
I am trying to use c++ templates to differentiate memory handling between int,float,short,char types,
and char*, char[1..n].
It works fine for primitive types e.g
CBasicData<int>, CBasicData<float>, CBasicData<char>...
It also works for CBasicData<char[2]>, but not for
CBasicData<char[8]>, CBasicData<char[16]>, CBasicData<char[32]>.
I need to generalize behviour for all string and char[] implementations.
How can I generalize it to work for any type of char[N], where N > 0 && < 1000.
Any help will be appreciated.
Thanks ,
Caz.

[source lang="cpp"]
// use this for char,int,float, and all primitive types
template < typename t > // Primary template
class CBasicData
{
public:

union Data
{
char m_cMemory[sizeof(t)];
t m_kValue;
}mData;
t GetValue() const { return mData.m_kValue; }
const char* GetMemory() { return mData.m_cMemory; }
void SetValue(const t& kValue ) { mData.m_kValue = kValue ; }
unsigned GetSize() { return sizeof(mData.m_kValue); }
};

// use this for char*, char[1..n], strings
// and or char[4]
// for char[9]
// for char[38]
// for char[2]

template< >
class CBasicData < char[2] > // Secondary template. This only works for char[2], but should be same for char[1..n]
{
std::string m_strValue;
public:
const char* GetMemory() { m_strValue.c_str(); }
const std::string& GetValue() const { return m_strValue; }
void SetValue(const std::string& rstrValue ) { m_strValue = rstrValue ; }
unsigned GetSize() { return m_strValue.length(); }
};

[/source]

Share this post


Link to post
Share on other sites
What does this class accomplish? What `GetMemory' does can be achieved with a reinterpret_cast. Edited by alvaro

Share this post


Link to post
Share on other sites
This seems to work:
[code]template<size_t N>
class CBasicData < char[N] > // Secondary template.
{
std::string m_strValue;
public:
const char* GetMemory() { m_strValue.c_str(); }
const std::string& GetValue() const { return m_strValue; }
void SetValue(const std::string& rstrValue ) { m_strValue = rstrValue ; }
unsigned GetSize() { return m_strValue.length(); }
};
[/code] Edited by alvaro

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1343231886' post='4962969']
This seems to work:
[code]template<size_t N>
class CBasicData < char[N] > // Secondary template.
{
std::string m_strValue;
public:
const char* GetMemory() { m_strValue.c_str(); }
const std::string& GetValue() const { return m_strValue; }
void SetValue(const std::string& rstrValue ) { m_strValue = rstrValue ; }
unsigned GetSize() { return m_strValue.length(); }
};
[/code]
[/quote]

Thanks mate. It does work, however I am a bit short on explaination as why it works.Could you please point me to any reference.
Cheerio.

Share this post


Link to post
Share on other sites
I am not sure what part surprises you. Is it the fact that I am using an integer type as a template parameter? That's perfectly standard.

I don't quite know all the details of how partial template specialization works, but the way I wrote that is the first thing that came to mind, and it happened to work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this