C preprocessor: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Tags: Mobile edit Mobile app edit iOS app edit App section source
Line 440:
The include directive limits code structure since it only allows including the content of one file into another. More modern languages support a [[modular programming|module]] concept that has public symbols that other modules import {{endash}} instead of including file content. Many contend that resulting code has reduced boilerplate and is easier to maintain since there is only one file for a module; not both a header and a body. [[C++20]] adds [[Precompiled header#Modules|modules]], and an <code>import</code> statement that is not handled via preprocessing.<ref>{{cite web|url=https://isocpp.org/files/papers/n4720.pdf|title=N4720: Working Draft, Extensions to C++ for Modules|archive-date=2019-04-30|archive-url=https://web.archive.org/web/20190430095053/https://isocpp.org/files/papers/n4720.pdf|url-status=live}}</ref><ref>{{cite web|url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1857r1.html|title=P1857R1 – Modules Dependency Discovery}}
</ref> Modules in C++ compile faster and link faster than traditional headers,<ref>{{cite web|url=https://learn.microsoft.com/en-us/cpp/build/compare-inclusion-methods?view=msvc-170|title=Overview of modules in C++|date=12 February 2022 |publisher=Microsoft}}</ref> and eliminate the necessity of [[include guard|{{mono|#include}} guards]] or [[pragma once|{{mono|#pragma once}}]]. Until C++26, <code>import</code>, <code>export</code>, and <code>module</code> keywords were partially handled by the preprocessor.
 
For code bases that cannot migrate to modules immediately, C++ also offers "header units" as a feature, which allows header files to be imported in the same way a module would. Unlike modules, header units may export macros, offering minimal breakage between migration. Header units are designed to be a transitional solution before totally migrating to modules. For instance, one may write <code>import <string>;</code> instead of <code>#include <string></code>, or <code>import "MyHeader.hpp";</code> instead of <code>#include "MyHeader.hpp"</code>. Most build systems, such as [[CMake]], do not currently support this feature.
 
== See also ==