Ill-Advised C++ Rant, Part 2
codersnotes.com
Ill-Advised C++ Rant, Part 2
1–10 of 66 posts
Re: Ill-Advised C++ Rant, Part 2
#2Re: Ill-Advised C++ Rant, Part 2
#3do you realize this would crash with empty array: #define countof(X) (sizeof(X) / sizeof((X)[0]))
Re: Ill-Advised C++ Rant, Part 2
#4do you realize this would crash with empty array: #define countof(X) (sizeof(X) / sizeof((X)[0]))
A much better implementation would be
using std::begin, std::end;
#define countof(x) (end(x) - begin(x))
(except for the double evaluation, but this is better as a template function instead of a macro anyway) because it'll work on any type that supports random-access iterators which include arrays but also std::array and vector.Re: Ill-Advised C++ Rant, Part 2
#5That said, there are some reasonable points, like '#pragma once' and 'enum-to-string' (extreme disagreeage on the 'string-to-enum' direction tho).
Re: Ill-Advised C++ Rant, Part 2
#6Re: Ill-Advised C++ Rant, Part 2
#7do you realize this would crash with empty array: #define countof(X) (sizeof(X) / sizeof((X)[0]))
No, because the expression is not evaluated at any time. Only the type of the expression is used. A much better implementation would be using std::begin, std::end; #define countof(x) (end(x) - begin(x)) (except for the double evaluation, but this is better as a template function instead of a macro anyway) because it'll work on any type that supports random-access iterators which include arrays but also std::array and…
Re: Ill-Advised C++ Rant, Part 2
#8A bunch of those are of the form 'you know there are better ways to go about doing that sort of thing' (like 'maybe consider using a std::vector instead of that static array' and 'just make a minimal ctor for initialization, you'll save typing in the long run' and 'when do you need to know the largest value in an enum, and why can't you just toss in a dummy last element, also why aren't you using enum class') if not…
Re: Ill-Advised C++ Rant, Part 2
#9A bunch of those are of the form 'you know there are better ways to go about doing that sort of thing' (like 'maybe consider using a std::vector instead of that static array' and 'just make a minimal ctor for initialization, you'll save typing in the long run' and 'when do you need to know the largest value in an enum, and why can't you just toss in a dummy last element, also why aren't you using enum class') if not…
Re: Ill-Advised C++ Rant, Part 2
#10A bunch of those are of the form 'you know there are better ways to go about doing that sort of thing' (like 'maybe consider using a std::vector instead of that static array' and 'just make a minimal ctor for initialization, you'll save typing in the long run' and 'when do you need to know the largest value in an enum, and why can't you just toss in a dummy last element, also why aren't you using enum class') if not…
What possible use is there for being able to convert the name of an enum as a string that isn't totally bug-ridden the minute you write it?