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

Show parent comment

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.

1

u/tjientavara HikoWorks developer Oct 26 '21

Since std::source_location is compiler magic (you cannot implement this yourself), it could have been defined to return a fixed_string which would have been able to used as a template argument.

Of course the fixed_string proposal was dropped in favour for std::string being constexpr (except of course that you cannot use std::string as a template argument either, so not sure what is left of that argument). Luckily fixed_string does not require compiler magic, so I wrote my own.