When using __DATE__
or __TIME__
in a header file, the results of the preprocessor for that header inclusion can be somewhat different.
Under which circumstances does using __DATE__
or __TIME__
in a header file violate the one-definition-rule?
As a follow-up: Does the assert
header violate the one-definition-rule?
If
__TIME__
gives different results for different translation units, then it must not be used in a context where the same result is required across translation units. This means e.g. initialising an object (e.g. a class member) to__TIME__
, where that initialiser is part of a header that gets included in multiple translation units, is going to be problematic.__DATE__
is less likely to give different results for different translation units if you start a fresh build, but incremental builds, ones that only recompile the files that changed, do make it likely to become a problem as well.assert
is a macro that expands differently depending on howNDEBUG
was defined when its header was included, so either the whole project must agree on whetherNDEBUG
should be defined, or functions defined in headers should avoid usingassert
.