Ben*_*min 20 c++ stl forward-declaration
// 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);
Run Code Online (Sandbox Code Playgroud)
wil*_*ell 34
你不能.#include <string>,你(几乎)别无选择.
原因是wstring在命名空间中定义std并且是typedef 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.因为(除少数例外)标准禁止向命名空间std(17.4.3.1/1)添加定义或声明,最终您无法以符合标准的方式转发声明任何标准模板或类型.具体来说,这意味着您无法转发声明std::wstring.
是的,我们都同意这将是方便有一个<stringfwd>头,像<iosfwd>对<iostream>.但事实并非如此.尽管如此,<string>它也不像编译那样硬核<iostream>.您有两种选择:#include<string>或使用不透明指针.