|
|
Subscribe / Log in / New account

enum conversion

enum conversion

Posted Oct 11, 2024 3:25 UTC (Fri) by NYKevin (subscriber, #129325)
In reply to: enum conversion by farnz
Parent article: Improving bindgen for the kernel

Yes, you can do that, but in practice what people actually do is much worse, usually one of the following:

* Force everything to use a Factory or Builder interface (you too can write Java EE in C++ if you try hard enough).
* Create the object in an "empty" state and then initialize/populate it as a separate step after it has been constructed (the object's empty state is exposed to client code, and not just in a moved-from temporary variable that nobody is going to look at or use, so now all client code has to defensively account for the possibility of an empty object).

There is also the saner option of making the constructor private and exposing a public static method that constructs instances in one step, with validation, and returns std::optional. But the latter was only introduced in C++17, so it's hardly idiomatic in preexisting codebases.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds