• Advertisement
Sign in to follow this  

c++ template how to

This topic is 2039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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:
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(); }
};
Edited by alvaro

Share this post


Link to post
Share on other sites

This seems to work:
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(); }
};



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
Sign in to follow this  

  • Advertisement