I am new to C++, is it just me or is the checklist kinda crazy? How often do you encounter these or plan on making use of them like the newer C++26 features like contracts? Looking for more experienced dev opinions...
I know Python and have been binging C++ for a couple of days now from scratch. C++ feels like a language where you gradually discover a huge checklist of "STATE YOUR INTENT" due to years of probably layering on things to shore up its weaknesses of memory/type safety etc. Like I get it, it's how C++ was designed for things to be explicit and it's like the English language where you don't need to know every word and in this case, feature, but every line of code makes me feel I'm asking a gajillion questions.
So far I have jotted down some stuff that took me awhile to digest...
* const
* constexpr
* [[nodiscard]]
* std::expected
* delete("reason")
* smart pointers
* zero-initialization curly brackets
* structured bindings
* assert
* static assert
* contracts
So I guess I'm not very familiar with the ecosystem or ever worked with other C++ code. Like is this good or bad? I would think it's very safe but do and should people code actually C++ like this? I don't have a frame of reference to relate to and I don't know if the C++ community is going to jump on things like C++26's contracts or reject it. What's the current attitude? If you were a teammate, would you love or hate to see something like this?
[[nodiscard]] constexpr std::expected<int, Error> GetHP(const std::unique_ptr<Player>& player)
{
assert(player);
if (!player) return std::unexpected(Error::NullPlayer);
const auto [hp, alive] = std::pair{player->hp, player->hp > 0};
static_assert(sizeof(int) == 4);
return alive ? hp : 0;
}
https://redd.it/1sqhobx
@r_cpp
Post #25016
14