Live data from Hacker News

Ill-Advised C++ Rant, Part 2

codersnotes.com

1–10 of 66 posts

Re: Ill-Advised C++ Rant, Part 2

#4

do 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 vector.

Re: Ill-Advised C++ Rant, Part 2

#5
A 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 outright 'jesus, don't put yourself in a position where you'd want to do that' (like the 'iterate over members of a struct' or the type stuff). And I had never heard of those 'fourcc' codes and have no idea why anybody would want them in a language standard.

That 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

#7
post #4

do 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…

There is such a function in the STL already.

http://en.cppreference.com/w/cpp/iterator/distance

Re: Ill-Advised C++ Rant, Part 2

#8

A 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?

Re: Ill-Advised C++ Rant, Part 2

#9

A 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…

Constructors are often brought up as a reaction to C99 initializers, but they're really not the same thing at all until and unless we get named function arguments in C++. Constructors as they exist are equivalent to the normal initializer syntax, which C++ does support.

Re: Ill-Advised C++ Rant, Part 2

#10

A 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?

Log output.
Post reply on HN