bbm::string_literal

A string literal (include/util/string_literal.h) is a compile-time constexpr string implementation. It is used extensively in BBM to pass constexpr strings as a template literal.

template<string_literal STR>
  void foo(void)
{
  std::cout << STR << std::endl;
  std::cout << toTypestring( STR ) << std::endl;
}

foo<"TEST">();

The above example will print out “TEST” and indicate that STR has type const bbm::string_literal<5>. String literals support basis operations such as concatenation, comparison, and casting to a regular (zero terminated) C string:

template<size_t N = 1>
struct string_literal

iterator

inline constexpr const char *begin(void) const
inline constexpr const char *end(void) const

compare two literals (constexpr)

template<size_t M>
inline constexpr bool operator==(string_literal<M> str) const
template<size_t M>
inline constexpr bool operator==(const char (&str)[M]) const
template<size_t M>
inline friend constexpr bool operator==(const char (&str)[M], string_literal<N> lit)

less operator (constexpr)

template<size_t M>
inline constexpr bool operator<(string_literal<M> str) const
template<size_t M>
inline constexpr bool operator<(const char (&str)[M]) const
template<size_t M>
inline friend constexpr bool operator<(const char (&str)[M], string_literal<N> lit)

concat two literals (constexpr)

template<size_t M>
inline constexpr string_literal<N + M - 1> operator+(string_literal<M> str) const
template<size_t M>
inline constexpr auto operator+(const char (&str)[M]) const
template<size_t M>
inline friend constexpr auto operator+(const char (&str)[M], string_literal<N> lit)

Public Functions

inline constexpr string_literal(void)

Empty string.

inline constexpr string_literal(const char (&str)[N])

constructor

template<size_t S, size_t LEN = N - 1>
inline constexpr auto substr(void) const

get substring

inline constexpr operator const char*(void) const

cast to C string

Public Members

char value[N]

Public Static Attributes

static constexpr size_t size = N

Number of chars in the string_literal.

static constexpr bool empty = (N == 1)

true is empty (i.e., == ‘\0’)

Friends

inline friend std::ostream &operator<<(std::ostream &s, const string_literal<N> &str)

forward ‘value’ to stream

A custom literal sl exists which is needed when the template literal is defined as auto.

template<auto STR>
  void bar(void)
{
  std::cout << STR << std::endl;
  std::cout << toTypestring( STR ) << std::endl;
}

bar<"TEST"_sl>();  // just passing "TEST" will cause a compile error.

A type trait bbm::is_string_literal_v has been defined to detect string literals (e.g., when using auto as template type).

Finally, include/util/to_string_literal.h contains a constexpr implementation to convert a (integer) literal to a string_literal:

template<auto N, int base = 10>
constexpr auto bbm::to_string_literal(void)

convert integrals to string_literal

Template Parameters:
  • N – = (integer) literal to covert into a string_literal

  • base – = conversion base; must be less than 16. Default base=10.