Cookie Cookie - 1 year ago 61
C++ Question

Exclude parts from compilation - still syntax-check

We have a pretty large project here in C++/.NET/Visual Studio, for performance testing we incuded some code that makes it possible to evaluate the execution time between certain points.

PERFORMANCE_TEST BeginMeasureTime("execute someCode")
PERFORMANCE_TEST EndMeasureTime("execute someCode")

Usually while developing the PERFORMANCE_TEST macro would be set to
so the evaluation code is switched off. So if code / interfaces etc. change and the code after the
macro becomes invalid, the developer will not notice that.

Is there any smarter way to have the performance evaluation code only built in a special version of the project but still make sure that it stays consistent? How would you normally do something like that?

Answer Source

One easy way would be to change




Then instead of


you could have

#define PERFORMANCE_TEST(x) {x;}
#define PERFORMANCE_TEST(x) if(0) { x; }

An optimizing compiler should generate no code for the second version if performance tests are disabled (or at the very least it'll not branch on the if), but it'll still be part of the compilation.

Alternatively, if build times aren't a huge concern you could simply always build both versions into separate directories.