Something went wrong. Try again.
A Tour of C++ for experienced programmers, as if C++26 is the only version that ever existed.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132#include <iostream>#include <cstddef>
// Fixed‑string non‑type template parameter.// Stores the characters of a literal at compile time.
template<std::size_t N>struct fixed_string { char buf[N]{}; // constexpr constructor copies characters. constexpr fixed_string(const char(&s)[N]) { for (std::size_t i = 0; i < N; ++i) buf[i] = s[i]; } // constexpr size query. constexpr std::size_t size() const { return N; }};
// Deduction guide from a string literal.template<std::size_t N>fixed_string(const char(&)[N]) -> fixed_string<N>;
// Example function taking a fixed_string NTTP.template<fixed_string S>void print_fixed() { std::cout << "fixed_string: " << S.buf << " (size=" << S.size() << ")" << std::endl;}
int main() { print_fixed<"hello">(); return 0;}