Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way to check, from a .hpp file, if C stdio functions are used in the corresponding .cpp file?

I have the following question. Supposing I have an header file header.hpp which is include in a test.cpp file. Is it possible to add instructions to the header.hpp file in order to check (maybe at compile time) if some C stdio functions are used in the test.cpp file and in positive case do something specific? For example something like:

header.hpp:

#ifndef HEAD
#define HEAD
#include <iostream>
if ( C stdio functions are used in test.cpp ){
  std::cout << "Hey" << "\n";
}
#endif

test.cpp

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include "header.hpp"
#include <cstdio>
int main(){
  printf( "Hello\n" ); // C stdio function has been used.
}

Output:

Hello
Hey

>Solution :

No, this is not possible. Neither C++, nor C, work like this, on a fundamental level.

An #include is logically equivalent to physically inserting the contents of the included file into the including file. Doing a cut and paste of your header.hpp into the beginning of your test.cpp replacing the #include accomplishes exactly the same thing.

The resulting code gets compiled from beginning to the end, in order.

When reading the header file, the C++ compiler has no knowledge, whatsoever, of something it hasn’t read yet. An #include stops reading the file that it’s in, and the included file gets read and compiled, before proceeding.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading