// 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
是在 namespace std
中定义的,并且已类型化为std::basic_string<wchar_t>
。更详细地说,std::wstring
是std::basic_string<wchar_t, std::char_traits<wchar_t> >
。这意味着要转发声明std::wstring
,您必须转发命名空间std::char_traits<>
中的std::basic_string<>
和std
。因为(除少数异常(exception)),该标准最终禁止向 namespace std
(17.4.3.1/1)添加定义或声明,所以您最终无法以符合标准的方式向前声明任何标准模板或类型。具体来说,这意味着您无法转发声明std::wstring
。
是的,我们都同意拥有<stringfwd>
header 会很方便,例如<iosfwd>
的<iostream>
。但是没有。 <string>
的编译难度也不及<iostream>
,但尽管如此。您有两种选择:#include<string>
或使用opaque pointer。
关于c++ - 前向声明std::wstring,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4801411/