问题描述
// This is a header file.
class MyClass; // It can be forward declared because the function uses reference.
// However, how can I do forward declaraion about std::wstring?
// class std::wstring; doesn't work.
VOID Boo(const MyClass& c);
VOID Foo(const std::wstring& s);
推荐答案
#include< string>
,您几乎没有选择。
原因是 wstring
在命名空间 std
中定义,并且被typedef定义到 std :: basic_string< wchar_t& code>。更精确地,
std :: wstring
是 std :: basic_string< wchar_t,std :: char_traits< wchar_t> >
。这意味着为了转发声明 std :: wstring
,你必须转发声明 std :: char_traits<>
和 std :: basic_string<>
在命名空间 std
内。因为(除了少数例外)标准禁止添加定义或声明到命名空间 std
(17.4.3.1/1)最终你不能转发声明任何标准模板或以符合标准的方式输入。具体来说,这意味着你不能转发声明 std :: wstring
。
The reason is that
wstring
is defined in namespace std
and is typedef'd to std::basic_string<wchar_t>
. More elaborately, std::wstring
is std::basic_string<wchar_t, std::char_traits<wchar_t> >
. This means that in order to forward-declare std::wstring
you'd have to forward-declare std::char_traits<>
and std::basic_string<>
inside namespace std
. Because (apart from a few exceptions) the standard forbids adding definitions or declarations to namespace std
(17.4.3.1/1) ultimately you can't forward-declare any standard template or type in a standard-conforming way. Specifically, this means you can't forward-declare std::wstring
.
是的,我们都同意将
。但没有。 < stringfwd>
头,比如< iosfwd>
>< iostream> < string>
也不像编辑< iostream>
您有两种选择:#include< string>
或使用。
And yes, we all agree it would be convenient to have a <stringfwd>
header, like <iosfwd>
for <iostream>
. But there isn't. <string>
is also not nearly as hardcore to compile as <iostream>
, but nevertheless. You have two choices: #include<string>
or use an opaque pointer.
这篇关于向前声明的std :: wstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!