【问题标题】:Clang error, no viable conversionClang 错误,没有可行的转换
【发布时间】:2025-12-26 18:25:12
【问题描述】:

我遇到了 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 上编译代码。

【问题讨论】:

  • 为什么不使用wchar_t 作为TCHARwchar_t 可能与 unsigned short aka char16_t 相同也可能不同。

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


【解决方案1】:

如果您想使用TCHAR,您必须扩展每个模板以使用它,包括wstringstream,您实际上需要basic_stringstream&lt;TCHAR&gt;,或者您可以:

typedef std::basic_stringstream<TCHAR> tstringstream;

【讨论】:

    最近更新 更多