[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/32bitmicro/cppfront/main/docs/cppfront/mixed.md [Back]  [Original]

# Mixing Cpp1 (today's C++) and Cpp2 in the same source file

## Compiling a source file that contains both Cpp1 and Cpp2 code

Cppfront compiles a `.cpp2` file and produces a `.cpp` file to be compiled by your favorite C++20 or higher C++ compiler.

The same `.cpp2` file may contain both Cpp2 syntax and today's "Cpp1" C++ syntax, **side by side but not nested**.

When cppfront compiles such a mixed file, it just passes through the Cpp1 code as-is, and translates the Cpp2 code to Cpp1 in-place. This means that when a call site (call this the "caller") uses a type/function/object (call this the "callee") written in the same file:

- **Code written in all Cpp2 is always order-independent by default.** When a caller written in Cpp2 syntax uses a callee written in Cpp2 syntax, they can appear in either order in the file.

- **Code written in Cpp1 is order-dependent as usual.** When either the caller or the callee (or both) are written in Cpp1 syntax, the callee must be declared before the caller.


## Okay: Cpp1 and Cpp2 side by side, interleaved

For example, this source file is fine, where the Cpp2 and Cpp1 code are side by side and seamlessly call each other directly as usual:

``` cpp title="mixed.cpp2  Mixing Cpp1 and Cpp2 code side by side in the same source file is okay" linenums="1" hl_lines="4-7"
#include                              // Cpp1
#include                           // Cpp1

N: namespace = {                                        // Cpp2
    hello: (msg: std::string_view) =                    // Cpp2
        std::cout 

Web Proxy Viewer  |  New URL  |  Original Page