在 VC++14 中使用 std::basic_stringstream<char16_t> 时出错

error while using std::basic_stringstream<char16_t> in VC++14

我正在尝试做一些基本的 char16_t 字符串 (u16string) 处理,运行 遇到了一些麻烦。本期短节目:

#include <string>
#include <sstream>

int main()
{
    int foo = 65;

    std::basic_stringstream<char16_t> ss;
    ss << foo;

    std::u16string s = ss.str();
}

产生错误:

Error   C2491   'std::numpunct<_Elem>::id': definition of dllimport static data member not allowed. xlocnum 259

我已经在一些在线编译器上试过了,但是没有错误。

感谢您提供的任何帮助!

好的,它看起来像是 VC++ 标准库或 VC++ 编译器中的错误,甚至可能是两者中的错误。

,第 85 行,在 class numpunct 内声明:

__PURE_APPDOMAIN_GLOBAL _CRTIMP2_PURE static locale::id id; // unique facet id

,第 258/259 行定义:

template<class _Elem>
    __PURE_APPDOMAIN_GLOBAL locale::id numpunct<_Elem>::id;

_CRTIMP2_PURE 定义为 _CRTIMP2,后者又定义为 __declspec(dllimport).

现在,根据我对VC++文档的阅读,应该可以了。 __declspec(dllimport) 允许用于静态声明。但是,静态 定义 不允许这样做。但是定义没有__declspec(dllimport),只有声明有。

尽管如此,还是产生了错误:编译器正在查看定义,将其视为 __declspec(dllimport),并产生错误。

我不确定是编译器错误还是库错误的原因是编译器还会发出警告,抱怨声明和定义不匹配——那个是 __declspec(dllimport)而另一个不是。由于根据文档,定义不能是 __declspec(dllimport),这向我建议 声明 定义应该是 __declspec(dllimport).

如果我们看看其他类似的成员,这个怀疑就得到了证实。例如,num_get::id 不是 _CRTIMP2_PURE,也不是 num_put::id

所以我认为有两种可能。一是 _CRTIMP2_PURE 有误,应将其删除。另一个是编译器在声称定义为 __declspec(dllimport) 时发出错误诊断,而实际上它不是。

无论如何,我认为代码示例应该可以编译,这是 Microsoft 需要修复的问题。