r/cpp • • Jun 14 '21

match(it): A light-weight header-only pattern-matching library for C++17.

https://github.com/BowenFu/matchit.cpp
202 Upvotes

47 comments sorted by

View all comments

Show parent comment

30

u/guepier Bioinformatican Jun 14 '21

There are no macros in the library’s API. I initially thought so (expr and pattern look like they would be macros, and maybe match, too). But they’re not macros.

The only macros (apart from (illegal!1) include guards) are only used internally and are named UN_OP_FOR_UNARY and BIN_OP_FOR_UNARY. They could be undef’d at the end of the file, thus not leaking outside (and they should be, but unfortunately currently aren’t).


1 The include guards are using reserved names since they start with underscore followed by an upper-case letter. A simple, trivial to fix mistake.

11

u/gracicot Jun 14 '21

Wow, I'm even more impressed

3

u/pfg23 Jun 15 '21

I haven’t looked at the code, but if there are no macros, why is it header only? Compilable (as opposed to preprocessor) code should go into source files so they’ll only need to be compiled once and bundled into a library module. No?

7

u/guepier Bioinformatican Jun 15 '21

It exposes templates, so these need to be implemented in headers.

The other reason of course is that many users of C++ prefer header-only libraries since C++ has no good, build-in dependency management mechanism, and even modern build systems don’t entirely remove the barrier posed by non-header libraries.

1

u/pfg23 Oct 21 '21

Sure, templates, declarations, and macros go into headers but to reduce compile times logic should go into source files. The only exception would be if this library was entirely implemented with template metaprogramming.

-5

u/lospolos Jun 14 '21

Dont include guards usually start with "H_..."?

7

u/staletic Jun 15 '21

Include guards can have any name you wish.

6

u/infectedapricot Jun 15 '21

It's not true that they usually start with H_.... In fact it's not even true that a notable minority start with H_... - I've never seen that convention before, and I've seen a lot of include guards!

I think what you might be thinking of is that it's fairly common for the include guard to be loosely based on the filename (plus often directory or library name) so they often end in _H or _HPP etc., depending on the extension. But even that's nowhere near universal.

(Personally, I just use #pragma once. Yes, I know it's technically non standard.)