Skip to content

Recommend #pragma once for self-iteration #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions doc/topics/file_iteration.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,18 @@ <h4>
#ifndef SAMPLE_H
#define SAMPLE_H

#include &lt;boost/config.hpp&gt; // BOOST_HAS_PRAGMA_ONCE
#include &lt;boost/preprocessor/iteration/iterate.hpp&gt;

template&lt;int&gt; struct sample;

#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 5, "sample.h"))
??=include BOOST_PP_ITERATE()

#ifndef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#endif // SAMPLE_H

#else
Expand All @@ -213,6 +218,17 @@ <h4>
#endif
</pre>
</div>
<div>
Although not necessary for correctness, <code>#pragma once</code> is
added after <b>BOOST_PP_ITERATE</b> to trigger the <i>multiple-include
optimization</i>, allowing compilers to avoid subsequent includes of
"sample.h" to improve build speed. In this instance the the include guard
is not sufficient to trigger the multiple-include optimization as is it not
the first preprocessor directive occurring in the file.
<code>#pragma once</code> must occur <i>after the last top-level</i>
<b>BOOST_PP_ITERATE()</b>, after which "sample.h" will no longer
self-include.
</div>
<div>
Using the same file like this raises another issue.&nbsp; What happens when a
file performs two separate file iterations over itself?&nbsp; This is the
Expand All @@ -227,6 +243,7 @@ <h4>
#ifndef SAMPLE_H
#define SAMPLE_H

#include &lt;boost/config.hpp&gt; // BOOST_HAS_PRAGMA_ONCE
#include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
#include &lt;boost/preprocessor/repetition/enum_params.hpp&gt;
#include &lt;boost/preprocessor/repetition/enum_shifted_params.hpp&gt;
Expand Down Expand Up @@ -257,6 +274,10 @@ <h4>
#define BOOST_PP_ITERATION_PARAMS_1 (4, (2, TYPELIST_MAX, "sample.h", 2))
??=include BOOST_PP_ITERATE()

#ifndef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#endif // SAMPLE_H

#elif BOOST_PP_ITERATION_FLAGS() == 1
Expand Down Expand Up @@ -331,6 +352,10 @@ <h4>
#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, EXTRACT_MAX, "extract.h"))
??=include BOOST_PP_ITERATE()

#ifndef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#endif // EXTRACT_H

#else
Expand Down Expand Up @@ -460,6 +485,10 @@ <h4>
#define BOOST_PP_ITERATION_PARAMS_1 (3, (1, 2, "file.h"))
??=include BOOST_PP_ITERATE()

#ifndef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#endif // FILE_H

#elif BOOST_PP_ITERATION_DEPTH() == 1
Expand Down Expand Up @@ -666,6 +695,10 @@ <h4>

??=include BOOST_PP_ITERATE()

#ifndef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#endif // FILE_H

#else
Expand Down Expand Up @@ -938,6 +971,10 @@ <h4>
??=include BOOST_PP_INCLUDE_SELF()
#endif

#ifndef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

// iteration over cv-qualifiers
#elif BOOST_PP_ITERATION_DEPTH() == 1 \
&amp;&amp; BOOST_PP_ITERATION_FLAGS() == 1 \
Expand Down
6 changes: 6 additions & 0 deletions test/iteration.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#
# if !BOOST_PP_IS_ITERATING
#
# include <boost/config.hpp>
#
# include <boost/preprocessor/cat.hpp>
# include <boost/preprocessor/comparison/equal.hpp>
# include <boost/preprocessor/control/expr_iif.hpp>
Expand Down Expand Up @@ -177,6 +179,10 @@
#
# undef ITER50SA
#
# ifndef BOOST_HAS_PRAGMA_ONCE
# pragma once
# endif
#
# endif
#
# else
Expand Down