Live data from Hacker News

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

foonathan.net

21–30 of 65 posts

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

#21
post #2

How can we replace nested comments? You can't comment out code that contains /* */ in it except with #if 0 Also, why is std::experimental::source_location loc = std::experimental::source_location::current(); loc.line better than __LINE__? what an unreadable monster that is!

Presumably it would become auto loc = std::source_location::current(); at some point, which seems fair enough to me.

It is since C++20. Comment author tries to make code verbose to make their point or does not know about auto and the state of C++.

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

#22
post #2

How can we replace nested comments? You can't comment out code that contains /* */ in it except with #if 0 Also, why is std::experimental::source_location loc = std::experimental::source_location::current(); loc.line better than __LINE__? what an unreadable monster that is!

Presumably it would become auto loc = std::source_location::current(); at some point, which seems fair enough to me.

I mean, don't love the macros - but that still looks like a load of typing to me - and it even reads less well than __LINE__ if you are looking at from a literate programming perspective.

It's tidier for the compiler though, but the change does not seem to make it easier for the reader of the code to comprehend.

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

#23
post #2

How can we replace nested comments? You can't comment out code that contains /* */ in it except with #if 0 Also, why is std::experimental::source_location loc = std::experimental::source_location::current(); loc.line better than __LINE__? what an unreadable monster that is!

The idea is to get the location of the caller without using a macro.

D just uses __LINE__, because it hasn't got a preprocessor so the compiler can resolve the token properly.

The implementation of that is at https://github.com/dlang/dmd/blob/v2.094.2/src/dmd/expressio...

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

#24

Earlier quoted context omitted.

Presumably it would become auto loc = std::source_location::current(); at some point, which seems fair enough to me.

It is since C++20. Comment author tries to make code verbose to make their point or does not know about auto and the state of C++.

I simply copied it from the article (minus the surrounding function).

Fortunately you only need to write this at the utility function, not at every call site, so it's ok.

In general I find various new features in C++ suffer from verbosity, but of course without experimental this one gets better.

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

#25

TBH the length that C++ goes to replace every single use of the preprocessor "just because" is close to zealotry. Every single "fix" probably requires more lines of code under the hood than the entire preprocessor and in the end you have added tons of additional features to the language to fix problems that (often) don't need fixing. The preprocessor being a simple text replacement tool is a feature, not a bug, but l…

The reason I don't agree with this is because the preprocessor is the main killer of compiler throughput in a large project, preventing a number of optimizations that would otherwise be possible.

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

#26

TBH the length that C++ goes to replace every single use of the preprocessor "just because" is close to zealotry. Every single "fix" probably requires more lines of code under the hood than the entire preprocessor and in the end you have added tons of additional features to the language to fix problems that (often) don't need fixing. The preprocessor being a simple text replacement tool is a feature, not a bug, but l…

Overuse of the preprocessor is perhaps the #1 cause of inscrutable compile error messages.

And certainly doesn't help with compile times.

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

#28

We just need an interpreter that can work on C++ files, supported by the standard. Something akin to the https://www.python.org/dev/peps/pep-0638/ Code generators/transformers are rare only because it's so hard to actually start. Too bad https://www.circle-lang.org never took off.

A few years ago Herb Sutter proposed Python-like metaclasses in C++ through compile-time code generation. Not sure if anything has been proposed for general use.

https://www.youtube.com/watch?v=4AfRAVcThyA

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

#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 (oh my) Maven profiles for conditional compilation (among other things).

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

#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 solving these sorts of problems. (https://github.com/ocaml-community/cppo)
Post reply on HN