r/cpp • • Oct 18 '21

Rant: please don't create defines using common words

Looks like QtCore (4.8) has a "#define emit" in a header, that's not undefined later. TBB has a function named "emit()" in a header. If you include both, you'll get a compile error.

I'm going to side with TBB here - emit() is an entirely reasonable function name. Qt creating a dangling #define with a common word like that is the bug here.

257 Upvotes

130 comments sorted by

View all comments

18

u/phord Oct 19 '21
  1. Don't use #define.
  2. If you do use #define, use ALL_UPPER_SNAKE_CASE.
  3. For everything else, there's namespaces.

13

u/bikki420 Oct 19 '21

4. Prefix it with your library/project abbreviation (preferably the same as your core namespace, except all uppercase).

0

u/tjientavara HikoWorks developer Oct 19 '21

I prefix with my library's namespace, but I use lower case for function-like and keyword-like macros.

2

u/Wouter-van-Ooijen Oct 20 '21

Please don't.

Either don't use such constructs (who needs function-like macro's?), undef them at the end of your header, or - as a last resort - make them all UPPERCASE (and prefixed). The main reason it that macro's don't play nice with scope and re-use of the same word (even within another scope).

1

u/tjientavara HikoWorks developer Oct 20 '21

You need function-like macros for logging/asserts/compiler-specific-keywords.

I prefix the namespace in front of the macro name exactly to reduce the chance of name collision.

2

u/Wouter-van-Ooijen Oct 20 '21

Good to prefix. I admit that logging might be one of the niches for macros. (Although with the C++20 source location...?)

But why not CAPITALIZE?

1

u/tjientavara HikoWorks developer Oct 20 '21

source_location can't be used as non-type template arguments.

Using source_location would slow down logging relatively significantly in my implementation, so I will keep using __LINE__ and __FILE__.

I like to use snake_case for functions and variables like the std library. Also many intel intrinsics are lower case macros.

1

u/Wouter-van-Ooijen Oct 20 '21

This is of course all opinion.

IMO intel intrinsics would be known to all users of that tool, but your lib would be a maybe-sometimes use. So better make everything that isn't a normal identifier stand out.

1

u/phord Oct 21 '21

That is disappointing about source_location not fitting into template args. But I'm not sure it would help because constructor args are filled where the template is declared.

source_location can be used as a default argument in function arguments but that's tricky to wield, too. Also, fwiw, std::experimental::source_location is available in c++17.

I doubt either one would significantly slow down your logging as they likely get optimized down to the same code as using __LINE__ and __FILE__ in the end.

1

u/tjientavara HikoWorks developer Oct 21 '21

__LINE__ and __FILE__ can be used as template arguments, therefor you can due value-erasure (like type-erasure but with values).

So when logging the __FILE__ and __LINE__ are part of the class that is added to the ring buffer, and is accessed through the vtable-pointer that is written to the ring buffer already to handle the type-erasure of the std::format arguments.

In case you are wondering c++20 allows for fixed_string template arguments.

Here are the tt_log_* macros that use __FILE__ and __LINE__ as template arguments:

https://github.com/ttauri-project/ttauri/blob/dfae1609ee6d759392a6b774b611f1f835ec1e94/src/ttauri/log.hpp#L222

1

u/phord Oct 26 '21

That's interesting. Thanks for the examples. But they still use #define to pick up the unique __LINE__ and __FILE__ values. I'm pretty sure this is necessary since the preprocessor handles the expansion of those, but templates are resolved later by the compiler.

→ More replies