[英]Template method in C++ to assign wchar_t* or char* to array
我有以下问题。 我有一个模板方法,可以接受以下常规或long char*
数组:
template <class myType>
static void createArray(const myType* (&MyArray)[10])
现在,我想根据可以为char*
或wchar_t
的myType
变量为该数组分配值。 我创建了一个小的方法来告诉我myType
是否长,以便我可以分配类型,如下所示:
MyArray[1] = ( MyTemplate<myType>::isWide()) ? L"-1" : "-1";
不幸的是,我总是收到一个错误,我无法将wchar_t
分配给char
数组,反之亦然。 我不能使用字符串类。 有什么想法可能吗? 谢谢!
问题是在编译时 ,类型不匹配。 这是您想要的技巧:
template <typename t>
struct x {
static t *c;
};
template<>
char *x<char>::c = "-1";
template<>
wchar_t *x<wchar_t>::c = L"-1";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.