Igor R.
Igor R.

Reputation: 15075

Mixing NDEBUG - is it safe?

  1. Consider a C++ header file compiled both in my_lib.a and in my_prog that links with my_lib.a. The library was compiled without NDEBUG, while my_prog - with NDEBUG. Would it result in ODR violation?
  2. What if my_lib.so is a shared library? Of course, ODR is irrelevant here, because there are 2 separate executables, but could NDEBUG affect std (or other) classes in a way that would prevent passing their instances correctly via SO interface? E.g. if an std::vector instance was created in my_prog, can it be passed as an argument to the SO? May NDEBUG affect memory allocation etc?

Does the Standard specify this?

Upvotes: 1

Views: 230

Answers (1)

BoBTFish
BoBTFish

Reputation: 19767

20.5.2.2 Headers [using.headers]

  1. A translation unit may include library headers in any order (Clause 5). Each may be included more than once, with no effect different from being included exactly once, except that the effect of including either <cassert> or <assert.h> depends each time on the lexically current definition of NDEBUG.

It is guaranteed not to be an issue for standard headers, however the issue you have highlighted does apply to functions in source files you provide yourself.

6.2 One-definition rule [basic.def.odr]

  1. There can be more than one definition of a class [function/enum/variable/etc] provided the definitions satisfy the following requirements:

[...] each definition of D shall consist of the same sequence of tokens;

Note that tokenisation happens after preprocessing, so if the definition contains any assert, this must preprocess to the same token sequence, i.e. must have the same NEDBUG setting during compilation.

Upvotes: 1

Related Questions