Skip to content
ktox logo

ktox-cpp: Kotlin → C++ transpiler.

ktox-cpp allows you to write Kotlin code and transpile it to idiomatic C++ header files (.hpp), enabling the use of Kotlin's powerful language features (like classes, null safety, and scope functions) in C++ environments.

Key Features

  • Classes and Inheritance: Full support for Kotlin classes, constructors, init blocks, and interfaces.
  • Modern C++ output: Generates idiomatic C++ using std::optional, std::vector, std::unordered_map, and structured bindings.
  • Null Safety: Transpiles Kotlin's null-safety operators (?., ?:, !!) and smart casts (is, !is) into robust C++ checks.
  • Comprehensive Standard Library: Includes C++ implementations for dozens of Kotlin standard library functions:
    • Collections: map, filter, fold, reduce, groupBy, associateBy, zip, flatten, and more.
    • Scope Functions: let, run, with, apply, also, takeIf, takeUnless.
  • Operator Overloading: Full support for overloading arithmetic, comparison, and indexing operators.
  • Infix Functions: Supports both standard and user-defined infix functions.
  • Advanced Control Flow: if/when as expressions, and for loops with ranges (.., until, downTo, step).

Shortcomings & Limitations

The following items represent current shortcomings, missing features, or partial support in ktox-cpp.

Feature Status Description
finally blocks ⚠️ Partial Emitted as a comment + trailing code. C++ has no finally; use RAII destructors.
inner class ❌ Missing Outer pointer is not captured. Workaround: pass outer object manually.
sealed class ⚠️ Partial Emitted as abstract class; hierarchy enforcement is not performed at transpilation.
try as expression ❌ Missing Not supported; emits /* unsupported */. Restructure to var + try block.
Destructuring ❌ Missing val (a, b) = pair emits /* unsupported */. Use C++17 structured bindings manually.
Secondary Constructors ❌ Missing Not supported. Workaround: use factory functions.
Coroutines ❌ Missing suspend fun, async/await are not supported.
SAM conversion ❌ Missing Not supported.
is type check ⚠️ Partial Requires polymorphic types (virtual functions). Does not work for value types/primitives.
val + Mutable Collections ⚠️ Partial val m = mutableMapOf() emits const auto, preventing modification. Use var.
operator fun set ⚠️ Partial Cannot be emitted as operator[] (C++ restriction). Call as method obj.set(k, v).
Named Arguments ❌ Missing Ignored; all arguments are passed positionally.
Reified Generics ❌ Missing Not supported.