Reputation: 527
In some large C++ projects, there are many #include
directives. For example,
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
I have to write four #include
in this example. I am so lazy that I only want to write it once. Perhaps like this:
#include {
<iostream>
<boost/asio.hpp>
<boost/bind.hpp>
<boost/date_time/posix_time/posix_time.hpp>
}
Is there any way to do it like this? Or can we define a macro to do this?
Upvotes: 1
Views: 1180
Reputation: 5241
Here is a macro that you could use (I wouldn't want to though, it made a nice little exercise on writing macros though):
#define HASH #
#define F(X) X
#define INCLUDE(X) F(HASH)include <X>
#define FEI_1(X, ...) INCLUDE(X)
#define FEI_2(X, ...) INCLUDE(X) NEW_LINE FEI_1(__VA_ARGS__)
#define FEI_3(X, ...) INCLUDE(X) NEW_LINE FEI_2(__VA_ARGS__)
#define FEI_4(X, ...) INCLUDE(X) NEW_LINE FEI_3(__VA_ARGS__)
#define FEI_5(X, ...) INCLUDE(X) NEW_LINE FEI_4(__VA_ARGS__)
#define FEI_6(X, ...) INCLUDE(X) NEW_LINE FEI_5(__VA_ARGS__)
#define FEI_7(X, ...) INCLUDE(X) NEW_LINE FEI_6(__VA_ARGS__)
#define FEI_8(X, ...) INCLUDE(X) NEW_LINE FEI_7(__VA_ARGS__)
#define INCLUDE_BLOCK_(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define INCLUDE_BLOCK(...) INCLUDE_BLOCK_(__VA_ARGS__, \
FEI_8,FEI_7,FEI_6,FEI_5,FEI_4,FEI_3,FEI_2,FEI_1)(__VA_ARGS__)
If you stick this in a header file header_header
, then you will be able to use it as follows:
#include <header_header>
INCLUDE_BLOCK(
iostream,
boost/asio.hpp,
boost/bind.hpp,
boost/date_time/posix_time/posix_time.hpp)
/* Write Some Code Here */
This solution is not all that practical, in order to compile your code you now need to run the preprocessing stage twice and insert the NEW_LINE
characters.
gcc -E main.cpp -o main.e.cpp
sed -i 's/NEW_LINE/\n/g' main.e.cpp
g++ main.e.cpp -o a.out
It would be possible to extend this code to create a specific boost
include block that removes the need to keep adding the boost
part. One limit on this current implementation is that you can only include 8 headers.
Upvotes: 1
Reputation: 26409
You can't do that, because #include uses preprocessor, and preprocessor is dumb as rock.
You could, however, write your own additional preprocessor that takes *.cpp file as input and converts your extended #include syntax into standard syntax. Example of preprocessor is moc
Or you could put all includes into single file, but that'll add unnecessary dependencies and in the long run will increase compilation times.
Personally, I wouldn't bother.
Copy/paste means duplicating
Duplicating code (function bodies, for example) is bad. However, #include
directive is not technically a code.
Upvotes: 5
Reputation: 217145
Some IDE/text editor have custom code snippets. Check if you have a plug-in for your tool.
Upvotes: 0
Reputation: 3968
One solution would be to put all those in a file "files_to_be_included.h"
files_to_be_included.h
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
and then using
#include "files_to_be_included.h"
to include them all at once
Upvotes: 1
Reputation: 272467
Is there any way to do it like this?
No.
The best you could do is put all your #include
directives into a dedicated header file, and then #include
that single header everywhere it's needed.
Upvotes: 8