News for hackers. Stuff that matters.
Re:Is the preprocessor still needed in C++? (2017) by Aardwolf on Sunday December 06, 2020 @07:26AM (#25323075)
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) by sesuximo on Sunday December 06, 2020 @10:33AM (#25324114)
Source location is so much more than that macro!!

- it has file name, line number, and char number! That already makes the number of characters more similar if that’s your metric

- it can be forwarded/passed around. It’s much harder to pass macros around

- it can easily capture the caller’s location rather than the location of the macro

Re:Is the preprocessor still needed in C++? (2017) by PaulDavisThe1st on Sunday December 06, 2020 @12:22PM (#25324929)
Since #define will still exist, I'd suggest

   #define __WHERE__ std::source_location::current()
:)
Re:Is the preprocessor still needed in C++? (2017) by MauranKilom on Sunday December 06, 2020 @07:54AM (#25323201)
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) by jpz on Sunday December 06, 2020 @08:53AM (#25323448)
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) by unglaublich on Sunday December 06, 2020 @08:50AM (#25323441)
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) by Aardwolf on Sunday December 06, 2020 @08:55AM (#25323457)
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) by identity0 on Sunday December 06, 2020 @10:35AM (#25324122)
auto in function signatures is just really, really stupid.
Re:Is the preprocessor still needed in C++? (2017) by mhh__ on Sunday December 06, 2020 @08:55AM (#25323456)
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) by layer8 on Sunday December 06, 2020 @07:32AM (#25323097)
> 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) by eugene3306 on Sunday December 06, 2020 @07:41AM (#25323145)
> How can we replace nested comments?

you may use multiline string literals

Re:Is the preprocessor still needed in C++? (2017) by tkln on Sunday December 06, 2020 @08:37AM (#25323394)
Care to elaborate on how that's supposed work in practice?
Re:Is the preprocessor still needed in C++? (2017) by scatters on Sunday December 06, 2020 @09:46AM (#25323768)
Wow, ugly, but it works:

    (void) R"long-comment(
    /* C-style comment */
    std::cout << "hello" << std::endl;
    )long-comment";
Re:Is the preprocessor still needed in C++? (2017) by ksherlock on Sunday December 06, 2020 @11:41AM (#25324580)
Ugly, confusing, hard to undo, and only works within a function (since it's a statement).