Live data from Hacker News

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

foonathan.net

1–10 of 65 posts

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

#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!

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

#3
TLDR of the article: the new C++ features can replace some macro usages, but not all.

"With current C++(17), most of the preprocessor use can’t be replaced easily."

"And even then: I think that proper macros, which are part of the compiler and very powerful tools for AST generation, are a useful thing to have. Something like Herb Sutter’s metaclasses, for example. However, I definitely don’t want the primitive text replacement of #define."

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

#4
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!

> How can we replace nested comments?

s/^/\/\// (and the reverse) work well for me. It nests.

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

#5
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!

> How can we replace nested comments?

you may use multiline string literals

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

#6
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.

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

#7
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.

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

#8
Just yesterday I had to wrap up offsetof in a macro for use in some pseudo-reflection code

    #define MEMBER(C, M) { offsetof(C, M), sizeof(C::M) }
I couldn't figure out nice a way to do this without the preprocessor. The best I came up with was to use a lambda:

    [] (const C& c) { return std::cref(c.m); }
But these are stored in a std::map which means I have to use function pointers or accept the overhead of std::function

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

#9
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 like every universal tool it requires some common sense to not abuse it.

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

#10

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.

Isn’t that what templates and constexpr are?
Post reply on HN