Open vittorioromeo opened 4 years ago
Note that most of the changes above are C++11 friendly. [[nodiscard]] unfortunately isn't, but pretty much every compiler has an alternative which works even in C++03 mode. E.g. https://stackoverflow.com/questions/4226308/msvc-equivalent-of-attribute-warn-unused-result
OpenVR uses Modern C++ constructs already (see #1314), but the API is suboptimal in terms of safety. Some examples:
C-style includes are being used (e.g.
stdint.h
instead ofcstdint
). This helps with C++ Standard conformance.constexpr
is not being used for compile-time constants. It is stronger thanconst
as it guarantees that the initialization expression is known at compile-time. E.g.typedef
is being used instead ofusing
. The latter is more readable. E.g.enum
is used instead ofenum class
, leading tovr::
namespace pollution and unwanted implicit conversions. E.g.vr::
namespace. E.g.[[nodiscard]]
attribute is not being used. It is an extremely important attribute that can help avoid critical bugs, as it warns when a return value is not used. Most of the return values in the OpenVR API should be inspected by the caller, and not ignored! That is a source of common bugs. E.g.All the above are changes that do not require any standard library support, and can really improve the readability and safety of the API (especially
[[nodiscard]]
). There are also other possible improvements that require library support, like usage ofstd::string_view
instead ofconst char*
orstd::string
. That could lead to performance improvements in cases like the following:I could make a fork of the API targeted to Modern C++ users, but I'd rather improve
openvr
instead of branching out. I am happy to contribute, but first I'd like to know what your policy is and see the outcome of #1314.