qxLib
string_literal.h
Go to the documentation of this file.
1 /**
2 
3  @file string_literal.h
4  @author Khrapov
5  @date 10.09.2021
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
12 
13 #include <algorithm>
14 
15 namespace qx
16 {
17 
18 /**
19 
20  @struct string_literal
21  @brief Literal wrapper for a constant expression string
22  @details Uses implicit conversion to allow templates to *seemingly*
23  accept constant strings like IsAnswer<"42">()
24  @tparam T - char type
25  @tparam N - char array length
26  @author Khrapov
27  @date 10.09.2021
28 
29 **/
30 template<class T, size_t N>
32 {
33  constexpr string_literal(const T (&str)[N]) noexcept
34  {
35  std::copy_n(str, N, value);
36  }
37 
38  static constexpr size_t size() noexcept
39  {
40  return N;
41  }
42 
43  constexpr const T* data() const noexcept
44  {
45  return &value[0];
46  }
47 
48  constexpr basic_string_view<T> view() const noexcept
49  {
50  return basic_string_view<T>(data());
51  }
52 
53  T value[N];
54 };
55 
56 } // namespace qx
Literal wrapper for a constant expression string.