In the previous process, we used LOCAL to repeatedly generate three template classes into three separate rows, but we still don't meet the requirement. After all, one class in one row is not easy to read and debug. For further improvement, we need to use the new method: File Duplication ).
As the name suggests, files need to be used for repetition. We write the pattern used for repetition to a file separately, and then call this pattern to repeatedly generate code.
First, create a separate file:pattern.hpp
:
#define n BOOST_PP_ITERATION()
#define TINY_print(~,~,data) data
template <BOOST_PP_ENUM_PARAMS(n, class T)>
struct tiny_size<
BOOST_PP_ENUM_PARAMS(n,T)
BOOST_PP_COMMA_IF(n)
BOOST_PP_ENUM(BOOST_PP_SUB(M,n), TINY_print, none)
> : mpl::int_<n> {};
#undef n
This file will not be used for compilation, but will be carried as a mode. We should note that there is no ''' symbol behind each line, because it is not used to generate a line of code, but to generate a code block. Macro FunctionsITERATION()
To obtain the duplicate index.
To trigger this mode, we write the following in our main code :(test.cpp
)
#include <boost/preprocessor/repetition.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/iteration/iterate.hpp>
#define M 3
#define BOOST_PP_ITERATION_LIMITS (0,M-1)
#define BOOST_PP_FILENAME_1 "pattern.hpp"
#include BOOST_PP_ITERATE()
Now we have two files. The main file istest.cpp
, The mode file ispattern.hpp
. It can be seen that the file is repeated in the same way as the LOCAL file. Two things need to be defined in advance: one is the repeated range LIMITS; the other is the Duplicated content, but this time it is not MACRO, the file name of the mode file. In addition, we noticed that the FILENAME macro is followed by a 1, which is used to specify the nested hierarchy of the loop and used for multi-layer nested loops.
The command line for compilation and execution has slightly changed:
> g++ -P -E -I. test.cpp > test.out.cpp
The difference is that there is one more-I parameter, which specifies the current directory as the header file search directory, because our mode file is in the current directory. If this parameter is not added, the pre-compiler cannot find the mode file.
Let's take a look at the pre-compiled output:
template <>
struct tiny_size<
none , none , none
> : mpl::int_<0> {};
template < class T0>
struct tiny_size<
T0
,
none , none
> : mpl::int_<1> {};
template < class T0 , class T1>
struct tiny_size<
T0 , T1
,
none
> : mpl::int_<2> {};
Yes. Each template class is scattered across multiple rows, which makes reading easier.
However, although the results are satisfactory, the introduced mode file adds to the burden of project management, so don't worry. boost also provides us with a file repetition improvement: Self-repetition. Its purpose is very direct: to eliminate this extra mode file.
In fact, self-Repetition does not introduce new macro functions. It only uses a small trick we often encounter. Remember the macros included in each header file:
#ifndef __THIS_HEADER_FILE__
#define __THIS_HEADER_FILE__