fixed_string: add a compile type string suitable for template params
This commit is contained in:
parent
03786f3fcc
commit
d69d3766ba
@ -329,6 +329,7 @@ list (
|
|||||||
extent.hpp
|
extent.hpp
|
||||||
fixed.cpp
|
fixed.cpp
|
||||||
fixed.hpp
|
fixed.hpp
|
||||||
|
fixed_string.hpp
|
||||||
float.cpp
|
float.cpp
|
||||||
float.hpp
|
float.hpp
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/format.cpp
|
${CMAKE_CURRENT_BINARY_DIR}/format.cpp
|
||||||
|
38
fixed_string.hpp
Normal file
38
fixed_string.hpp
Normal file
@ -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 <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
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 <std::size_t N>
|
||||||
|
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 <std::size_t N>
|
||||||
|
fixed_string (char const (&)[N]) -> fixed_string<N - 1>;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user