C ++: is it possible # to include functions inside the body?

I want to include the header file only when calling a specific function body?

Is this possible or recommended in C ++?

+4
source share
7 answers

Not.

This is a little wrong with you; #include is not processed at all at runtime. #include it is impossible to create a file based on the characteristics of the program; as soon as the program executes its source code, it is fixed (since it has already been compiled).

+11
source

Probably yes; Recommended not, not usually.

#include is a process, and the early stage of parsing works this way in many places, not considering the language context at the include point.

Please note that the inclusion will occur regardless of whether the function is called, so it probably will not solve the problem that you are trying to solve. The attached file will be placed directly inside the function body, so the included file must be designed to be included at such a point in the source file.

+7
source

You are clearly biased towards higher-level languages ​​such as Python, in which you can do things like:

 if ( a ): from whatever import whatever_I_need else: from whatever_else import whatever_I_need 

However, in C ++ this is evaluated at compile time (well, in fact, #include is a preprocessor directive and even evaluated before compilation). Putting this inside an if-else construct block will only lead to compilation errors. Just keep in mind that #include is just a way to dump the contents of a file (usually a header (interface) file) into another that requires its declarations. Very low level. It will be included anyway.

+2
source

You can # include inside the function body, but this does not mean that the header file is included only when the function body is called.

The contents of the header file will be included by the preprocessor at compile time, and therefore, the code will always be present in the function.

Obviously, the code is executed only when the function is called.

+1
source

While you can put #include inside the function body, this is not what you want to do. It is not possible to include the header file only if a specific function is called, because the inclusion occurs before compilation time, but the function is called at run time.

0
source

Well, the question was a little off track, but the actual issue is important.

If for some of your methods you really need to depend on a library that most users don’t like, it’s quite reasonable to be prepared to rid them of this unnecessary dependency.

You can do this by manipulating the preprocessor tokens.

 /// myClass.h /// In order to use BigDep, you need to: /// - define `MYPROJECT_BIGDEP_USE` before included this header /// - have `<bigdep-install-path>/include` in your include path /// - link with `libbigdep.so` class MyClass { public: void foo() const; #ifdef MYPROJECT_BIGDEP_USE void bigDep() const; #endif // MYPROJECT_BIGDEP_USE }; /// myClass.cpp #include "blah.h" #include "bar.h" #ifdef MYPROJECT_BIGDEP_USE #include <bigdep.h> #endif // MYPROJECT_BIGDEP_USE void MyClass::foo() const { /**/ } #ifdef MYPROJECT_BIGDEP_USE void MyClass::bigdep() { /**/ } #endif // MYPROJECT_BIGDEP_USE 

Thus, you must compile the mode, depending on whether the character MYPROJECT_BIGDEP_USE or not.

In gcc, you can define a character on the command line using -D , as in -DMYPROJECT_BIGDEP_USE . In Visual Studio, this is with /D , as in /DMYPROJECT_BIGDEP_USE .

Another option for the user is to wrap the header:

 // myProjectMyClass.h #define MYPROJECT_BIGDEP_USE #include <myClass.h> 

And only ever include "myProjectMyClass.h" and never head directly.

The use of these preprocessor directives is fairly common. For example, when installing the python cx_Oracle module, the installer checked the version of oracle that I used from my environment variables and disabled several methods that were not available to him directly at compile time.

0
source

In one program that I need to use in my application (using Qt), there is:

 int main(int argc, char *argv[]) { QApplication a(argc, argv); using namespace PL; "#"include "res/przypisania_txt.h" MainWindow w; w.show(); ... 

and when I insert include before main, it will not compile, so I gues you can use INCLUDE inside the function;)

0
source

Source: https://habr.com/ru/post/1304864/


All Articles