NOW LET US – AI RAG SaaS Studio TP.HCM
NOW LET US
Digital Product Studio
Back to news
DEV-TOOLS...2 min read

A new C++ back end for ocamlc

Share
NOW LET US Article – A new C++ back end for ocamlc

A new patch for ocamlc introduces a C++ backend that translates OCaml code into idiomatic C++ templates, effectively treating C++ as a purely functional language and using compilers like g++ as interpreters.

A New Step for the OCaml Compiler

The OCaml development community has just received a significant patch, adding a new C++ backend to the ocamlc compiler. This is considered a major improvement over the unincremented C currently used by the runtime and FFI.

This new backend allows for the direct translation of OCaml programs into idiomatic and readable C++ code. However, this approach comes with an interesting perspective: treating C++ as a purely functional programming language.

C++: A Purely Functional, Stateless Language

In the context of this new backend, C++ is operated as a purely functional language, with no support for mutable state. This leads to a limitation where the OCaml standard library is currently unavailable, as it contains many components based on mutation.

To illustrate, developers will need to reimplement modules (such as the List module) in a purely functional style to ensure compatibility when compiling to C++.

Using a C++ Compiler as an Interpreter

A unique aspect of this backend is how programs are executed. Instead of running an executable as usual, users utilize C++ compilers (like g++) acting as "interpreters."

By using options like -D to pass arguments into the main function, the program is executed during the compilation process itself. An interesting detail is that due to the historical development of C++, the program's output is displayed in the style of compiler error messages. This is a subtle homage to C++'s origins as an advanced preprocessor for C.

Furthermore, because C++ uses the :: operator for other purposes, OCaml's familiar infix :: syntax for list cons cells is replaced by explicitly nested Cons<hd, tl> structures in the output.

Performance and Resource Challenges

Leveraging the C++ template system for complex computations poses significant performance challenges. By default, larger programs are restricted, but users can expand this limit using the -ftemplate-depth=999999 option.

Real-world testing shows significant differences between compilers:

  • g++: Can compute prime numbers up to 10,000 in about half a minute, consuming approximately 11 GiB of memory. With more optimized data structures like leftist heaps, the time drops to 8 seconds with 3.1 GiB of memory usage.
  • clang++: Shows different performance, processing extremely fast but often ending with a warning or a segfault in similar scenarios.

Future Outlook

This approach opens the possibility of supporting more languages in the future. Developers expect that as soon as Rust finishes shipping support for "partial impl specialization," it too will become capable of running OCaml programs in a similar fashion to the current C++ backend.

© 2026 Now Let Us. All rights reserved.

Source: Hacker News

Advertisement
Ad slot ready: 5887729102

More in this category

NOW LET US Related – GLM 5.2 Is Out

dev-tools

GLM 5.2 Is Out

Zhipu AI has officially released GLM-5.2, its most powerful open-source model to date, featuring a 1M context window and advanced long-horizon task capabilities. The release underscores Zhipu's commitment to open-source AI and global scientific collaboration amid rising technological restrictions.

NOW LET US Related – Treating pancreatic tumours may have revealed cancer's master switch

dev-tools

Treating pancreatic tumours may have revealed cancer's master switch

A promising new drug called daraxonrasib has shown breakthrough results in treating pancreatic cancer, doubling median survival times. This achievement could pave the way for an entirely new class of cancer treatments.

NOW LET US Related – Leaving Mozilla

dev-tools

Leaving Mozilla

A poignant and candid reflection from a 15-year Mozilla veteran upon their departure. The author highlights the leadership's missteps in trying to emulate tech giants and urges Mozilla to return to its core values: community and uniqueness.

NOW LET US Related – Shepherd's Dog: A Game by the Most Dangerous AI Model

dev-tools

Shepherd's Dog: A Game by the Most Dangerous AI Model

A developer tested Anthropic's latest, supposedly 'too dangerous' AI model by asking it to build a long-held game idea in a single shot. The model succeeded, generating a complete 2,319-line game after a 45-minute reasoning session.

NOW LET US Related – Open source AI must win

dev-tools

Open source AI must win

If artificial intelligence becomes a utility rented only from a few closed institutions, humanity loses its operational freedom. Open-source AI is a vital infrastructure for the future of our digital society.

NOW LET US Related – Statement on US government directive to suspend access to Fable 5 and Mythos 5

dev-tools

Statement on US government directive to suspend access to Fable 5 and Mythos 5

The US government has issued an export control directive forcing Anthropic to suspend all access to its Fable 5 and Mythos 5 models due to national security concerns, a move the AI safety startup strongly disputes.

EXPLORE TOPICS

Discover All Categories

Deep dive into the specific technology sectors that matter most to you.