Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
cpp2_cppfront source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
cppfront trunk
Options
Source code
#include <cstddef> #include <iostream> #include <string> #include <string_view> namespace cpp2 { template<std::size_t N> class string_literal { public: // Typedefs using value_type = char; using size_type = std::size_t; using pointer = char*; using const_pointer = const char*; // ... /// Construct from a string literal only constexpr string_literal(const char(&str)[N + 1]) noexcept : data_{ str } {} // Just like `std::string`, the null terminator does not count towards the length of the string literal // Cast operators [[nodiscard]] constexpr operator std::string_view() const noexcept { return std::string_view{ data_, size() }; } [[nodiscard]] constexpr operator std::string() const { return std::string{ data_, size() }; } // Comparison operators // ... // Accessors // [[nodiscard]] constexpr const_iterator begin() const noexcept ... // [[nodiscard]] constexpr const_iterator cbegin() const noexcept ... // [[nodiscard]] constexpr const_iterator end() const noexcept ... // [[nodiscard]] constexpr const_iterator cend() const noexcept ... // [[nodiscard]] constexpr const_reference operator[](const size_type pos) const noexcept ... // [[nodiscard]] constexpr const_reference at(const size_type pos) const ... // ... [[nodiscard]] constexpr const_pointer data() const noexcept { return data_; } [[nodiscard]] constexpr const_pointer c_str() const noexcept { return data_; } // Rest of std::string interface // ... // Testers [[nodiscard]] static constexpr size_type size() noexcept { return N; } [[nodiscard]] static constexpr bool empty() noexcept { return N == 0; } private: const char(&data_)[N + 1]; // Reference to string literal of length N + null-terminator }; // Deduction guides template<std::size_t N> string_literal(const char(&)[N]) -> string_literal<N - 1>; } template<std::size_t N> print_sl: (str: cpp2::string_literal<N>) = { // Not entirely sure this is the correct Cpp2 syntax do use with NTTP's std::cout << str.c_str() << '\n'; } print_sv: (str: std::string_view) = { std::cout << str << '\n'; } print_str: (str: std::string) = { std::cout << str << '\n'; } print_chars: (str: *const char) = { std::cout << str << '\n'; } main: () -> int = { str: cpp2::string_literal = "a Cpp2 string literal"; // equivalent to str: = "a Cpp2 string literal"; sv: std::string_view = str; c_str:= str.c_str(); print_sl(str); print_sv(str); // implicit conversion to std::string_view print_str(str); // implicit conversion to std::string print_chars(str.c_str()); // must explicitly call .c_str() return 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree