Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include anywhere

Is the #include <file> meant to be used for headers only or is it simply a mechanical "inject this code here" that can be used anywhere in the code?

What if I use it in the middle of a cpp function to just "inject" code from a single source? will this work or will compilers scream about this?

like image 788
JasonGenX Avatar asked Apr 06 '11 14:04

JasonGenX


1 Answers

It is a mechanical inject the code here device. You can include a text file containing Goethe's Faust if you wish to. You can put it anywhere, even in the middle of a function (of course, #include needs a fresh line!).

However, it's strong convention to only use #include for header files. There may be reasons where I wouldn't object on it, for example pulling in machine-generated code or merging all translation units in a single file.

like image 127
Alexander Gessler Avatar answered Sep 25 '22 22:09

Alexander Gessler