Live data from Hacker News

Is the preprocessor still needed in C++? (2017)

foonathan.net

61–65 of 65 posts

Re: Is the preprocessor still needed in C++? (2017)

#61

Earlier quoted context omitted.

This is the reason yes, and why macro encapsulation by modules is so important

How do modules work with generics and cross-module inline functions? Probably I can find the answer in D or Rust but I am not familiar with their mechanisms. Thanks.

In Rust, the library format also includes a pre-compiled version of the generics needed, and so when the compiler includes the library, it can monomorphize from there.

Re: Is the preprocessor still needed in C++? (2017)

#62
post #10

Earlier quoted context omitted.

Isn’t that what templates and constexpr are?

To a degree, but templates and constexpr don't support a bunch of features like compile-time field enumeration and introspection + code generation. For example, let's say I have a bunch of structs: struct GeoCoordinate { int lat, long; }; struct GeoArea { std::vector perimeter; }; struct Place { std::string name; std::string contact_number; GeoArea area; }; Now I need to serialize these structs into a format to be se…

Yeah static structural reflection is an important use-case. The main objection I’ve heard to standardization in C++ has to do with maintaining the mistake of conflating structs and classes and supporting C’s long broken unit type.

Re: Is the preprocessor still needed in C++? (2017)

#63
post #30

Missing the most important case: Some external library you need changes a function signature and you need to be able to compile against the old or the new library, eg: #if LIBVERSION >= 2 draw_point (2, 3, RED); #else set_color (RED); draw_point (2, 3); #endif This is actually a case where the C preprocessor would be useful in many more languages. OCaml has cppo which is like a better cpp and is very useful for solvi…

[deleted]

Re: Is the preprocessor still needed in C++? (2017)

#64
post #37

Earlier quoted context omitted.

You can use "if constexpr" for that.

In this case this doesn't work because "if constexpr" parses both branches. If the new library version changes function signatures, the if-branch for the old library version produces an error: https://www.godbolt.org/z/4KK997 PS: interesting to note that Zig does the "right thing": https://www.godbolt.org/z/9c13PY

There was a proposal to do the correct thing and copy D's static if, but it was rejected for fairly contrived reasons IIRC.

Andrei Alexandrescu mentions it in a talk, if constexpr doesn't really do much of anything useful because it introduces a scope.

Re: Is the preprocessor still needed in C++? (2017)

#65
post #29

The best thing about C++ preprocessor is that it is dumb. Text goes in, text goes out. Easy to debug, simple rules. Anything I saw as an alternative either requires a significant amount of code, bending C++ rules, or specialized tools to see what is going on. Java tried so hard to "do the right thing" by abolishing the preprocessor, and we ended up with another preprocessor called IDE, unnecessary code patterns, and…

D has no preprocessor and has none of those problems.
Post reply on HN