C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allow forward declarations of typedef'ed classes

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Tue, 20 Feb 2024 15:31:31 +0000
As others already mentioned, this is impossible. However the library itself can provide forwarding headers to maintain API compatibility while providing forward declarations. Unfortunately not many libraries do this, including the standard library (except for iosfwd).



On 20 February 2024 09:49:08 GMT, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>I had a header file "record.h" something like as follows:
>
> class IDevice;
> int Record( IDevice * );
>
>I put in a forward declaration for 'IDevice, because I didn't want to
>include the header file that defines 'IDevice' for two reasons:
> (1) To reduce compile time
> (2) To avoid a circular dependency
>
>This worked fine until the person who maintained 'IDevice' did the
>following inside "IDevice.hpp":
>
> template<bool temp_param_record>
> class Interface {
> public:
> static consteval bool record(void) noexcept { return
>temp_param_record; }
> };
>
> typedef Interface<true> IDevice;
>
>So now any source file that does the following:
>
> #include "IDevice.h"
> #include "record.h"
>
>fails to compile. So I propose that the following be valid C++:
>
> class Monkey;
>
> int Func(Monkey *const p)
> {
> return nullptr == p;
> }
>
> class Donkey {
> int number;
> };
>
> typedef Donkey Monkey;
>
> int main(void)
> {
> Monkey obj;
> }
>
>I propose that we should be able to do a forward declaration of a
>typedef'ed class.
>--
>Std-Proposals mailing list
>Std-Proposals_at_[hidden]
>https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-02-20 15:31:36