Re: Replaces macros by metaprogramming
* Vincent R:
Cheers & hth.,
- Alf
Braidead or not it's used on MS platform so I did the following :
I would say that my question is not MS specific it's more about template
specializations and I think it's more adapted here !!!!
After if I handle a TCHAR or some exotic type what is the problem ?
So I wanted to say I tried this :
template <typename T>
struct IConv
{
int Strncpy(T dest, const wchar_t* src, int nLen) {;}
};
template <>
int IConv<char>::Strncpy(char* dest, const wchar_t* src, size_t nLen)
{
return ::wcstombs(dest, src, nLen);
}
template <>
int IConv<wchar_t>::Strncpy(wchar_t* dest, const wchar_t* src, size_t nLen)
{
return ::wcsncpy(dest, src, nLen);
}
But of course it doesn't work :
1>c:\cygwin-1.7\home\vincent\projects\gynoid\src\gynoid\src\UniConv.hxx(16)
: error C2244: 'IConv<T>::Strncpy' : unable to match function definition
to an existing declaration
1> with
1> [
1> T=char
1> ]
1>
c:\cygwin-1.7\home\vincent\projects\gynoid\src\gynoid\src\UniConv.hxx(9)
: see declaration of 'IConv<T>::Strncpy'
1> with
1> [
1> T=char
1> ]
1> definition
1> 'int IConv<T>::Strncpy(char *,const wchar_t *,size_t)'
1> with
1> [
1> T=char
1> ]
1> existing declarations
1> 'int IConv<T>::Strncpy(T,const wchar_t *,int)'
1> with
1> [
1> T=char
1> ]
You have 3 options:
A. Do as I suggested originally, and really, it'll save you a lot of work.
B. Make Strncpy a templated free-standing routine instead of a member routine.
C. Specialize the complete class: you can't specialize just a member routine.
With approach C you'd write e.g. (off the cuff)
template< typename Char > struct Conv;
template<> struct Conv<char>
{
// Tada!
};
template<> struct Conv<wchar_t>
{
// Todo!
}
Uh, except that the last one may cause DevStudio (I'm assuming that IDE because
of the Microsoft-specificity) to insist that you have some added work to do.
Which, in a sense, would be right, but for the wrong reason, as is usual when
apparently something is right... ;-)
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!