Forward declaration of std::wstring

前端 未结 5 1984
忘了有多久
忘了有多久 2021-02-05 08:00
// 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::wstri         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 08:24

    You can't. #include , you have (almost) no choice.

    The reason is that wstring is defined in namespace std and is typedef'd to std::basic_string. More elaborately, std::wstring is std::basic_string >. 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.

    And yes, we all agree it would be convenient to have a header, like for . But there isn't. is also not nearly as hardcore to compile as , but nevertheless. You have two choices: #include or use an opaque pointer.

提交回复
热议问题