Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: Is it possible to #include inside a function body?

Tags:

c++

I want to include a header file only if a certain function body is called?

Is this possible or recommended in C++?

like image 635
donalmg Avatar asked Mar 23 '10 12:03

donalmg


People also ask

Does a/b/c work in C?

In C, a == b == c is equivalent to (a == b) == c , where a == b yields 1 if true, 0 otherwise. In your case, a == b is true, so a == b == c is equivalent to 1 == c , which is false.

Is there such thing as C+?

C+ (grade), an academic grade. C++, a programming language. C with Classes, predecessor to the C++ programming language. ANSI C, a programming language (as opposed to K&R C)

Is C hard to master?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.


2 Answers

No.

You've got it a bit wrong; #include is not processed at run-time, at all. It's not possible to #include a file based on a program's execution characteristics; once the program executes its source is fixed (since it's already compiled).

like image 54
unwind Avatar answered Sep 21 '22 09:09

unwind


Possible, yes; recommended no, not usually.

#include is process and an early stage of parsing so works in many places with no regard for the language context at the point of the include.

Note that the include will happen regardless of whether the function is called so it probably isn't going to solve the problem that you are trying to solve. The file included will be placed directly inside the function body so the include file would have to be designed to be included at such a point in the source file.

like image 25
CB Bailey Avatar answered Sep 18 '22 09:09

CB Bailey