Hacker News new | ask | show | jobs
by Aardwolf 2020 days ago
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!

5 comments

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

Since #define will still exist, I'd suggest

   #define __WHERE__ std::source_location::current()
:)
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.

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.

auto in function signatures is just really, really stupid.
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...

> How can we replace nested comments?

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

> How can we replace nested comments?

you may use multiline string literals

Care to elaborate on how that's supposed work in practice?
Wow, ugly, but it works:

    (void) R"long-comment(
    /* C-style comment */
    std::cout << "hello" << std::endl;
    )long-comment";
Ugly, confusing, hard to undo, and only works within a function (since it's a statement).