Boost: duplicate files in the preprocessor library and self-repetition

Source: Internet
Author: User
Tags repetition

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 __

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.