Clang错误,没有可行的转换

时间:2012-10-18 13:29:22

标签: c++ gcc clang wchar-t char16-t

我正面临clang 3.1的问题。 GCC 4.2不会出现这个特殊问题。以下是发生错误的示例:

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>

typedef unsigned short char16_t;
typedef char16_t TCHAR;

typedef std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > wstringT;

template<class T>
inline wstringT MyTestFunction(const T& source)
{
std::wstringstream out;
out << source ;
return out.str();    // error occurs here
};

错误消息指出:

No viable conversion from '__string_type' (aka 'basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >') to 'wstringT' (aka 'basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> >')

使用编译器标志-fshort-wchar编译代码,该标志应该将wchar_t转换为16位无符号短路。我正在编译XCode v4.3.2上的代码。

1 个答案:

答案 0 :(得分:3)

如果你想使用TCHAR,你必须扩展每个模板以使用它,包括wstringstream你实际上想要basic_stringstream<TCHAR>,或者你可以:

typedef std::basic_stringstream<TCHAR> tstringstream;