From d69d3766bafe2262ba38f654dd02182ab9fc7e91 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 3 Dec 2019 09:33:39 +1100 Subject: [PATCH] fixed_string: add a compile type string suitable for template params --- CMakeLists.txt | 1 + fixed_string.hpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 fixed_string.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index dc20202d..92c6edd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -329,6 +329,7 @@ list ( extent.hpp fixed.cpp fixed.hpp + fixed_string.hpp float.cpp float.hpp ${CMAKE_CURRENT_BINARY_DIR}/format.cpp diff --git a/fixed_string.hpp b/fixed_string.hpp new file mode 100644 index 00000000..0dadd21c --- /dev/null +++ b/fixed_string.hpp @@ -0,0 +1,38 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019, Danny Robson + */ + +#pragma once + +#include + + +/////////////////////////////////////////////////////////////////////////////// +namespace cruft { + /// A simple string-like class that allows constexpr representation of + /// strings. + /// + /// This allows strings to be represented as instances of fixed_string in + /// template parameters (rather than using vendor specific array + /// extensions). + template + struct fixed_string { + char value[N + 1] {}; + + constexpr + fixed_string (char const _value[N+1]) noexcept + { + std::copy_n (std::begin (_value), N, std::begin (value)); + } + + constexpr operator char const* () const { return value; } + }; + + + template + fixed_string (char const (&)[N]) -> fixed_string; +}