2014-03-04 49 views
0

我遇到以下问题。我有接受的正常或long char*数组作为以下内容的模板方法:C++中的模板方法将wchar_t *或char *分配给数组

template <class myType> 
static void createArray(const myType* (&MyArray)[10]) 

现在我想取决于myType变量可以是char*wchar_t值分配给该数组。我创建了小法,告诉我阉myType长与否,这样我可以指定类型,就像这样:

MyArray[1] = (MyTemplate<myType>::isWide()) ? L"-1" : "-1"; 

不幸的是我总是说我不能指定wchar_tchar阵列,反之亦然错误。 我不能使用字符串类。 任何想法,如果这是可能的? 谢谢!

+0

此运算符在运行时不在编译时评估,因此是错误。 –

+0

我认为,解决方法是使用静态数组而不是C字符串 – basin

+0

'static myType [] s1 = {' - ','1',0};' – basin

回答

1

问题是在编译时,有一个类型不匹配。这是你想要的技巧:

template <typename t> 
struct x { 
    static t *c; 
}; 

template<> 
char *x<char>::c = "-1"; 
template<> 
wchar_t *x<wchar_t>::c = L"-1"; 
+0

魅力,谢谢! – Damian