ThePhD / future_cxx

Work done today for the glory of tomorrow - or, C and C++ systems programming papers.
https://thephd.dev/portfolio/standard
46 stars 8 forks source link

p1378 - std::is_string_literal(const T*) for C++29 #18

Open ThePhD opened 5 years ago

ThePhD commented 5 years ago

Determining whether a string is a string literal gives us certain guarantees about memory layout and null termination. The goal is to write a consteval function that covers this landscape area.

ThePhD commented 5 years ago

Note that is_static_storage_duration makes subtly different guarantees: notably, there may not be interning, it may not be a string literal, etc.

ThePhD commented 5 years ago

Submitted for LEWG, Köln, Germany 2019 for inclusion in C++23.

ThePhD commented 3 years ago

The utility of this is going to need more motivation; until then, it's going to be targeted at C++26 to take the pressure off.

ThePhD commented 2 years ago

Moved to C++29. It also might be impossible to implement at all, so it might be time to close this one entirely.

frederick-vs-ja commented 2 months ago

This seems (partially?) implementable if we can restore constant evaluation from failure.

template<class CharT>
consteval bool is_string_literal(const CharT* p) noexcept {
  try consteval { // constant evaluation failure inside the try-consteval block is caught and ignored
    while (*p != CharT{})
      ++p;
    return true;
  }
  return false;
}

Perhaps the __builtin_constant_p extension is already capatible for implementing the utility (Godbolt link)?

template<class CharT>
constexpr bool is_string_literal_helper(const CharT* p) noexcept {
  while (*p != CharT{})
    ++p;
  return true;
}

template<class CharT>
consteval bool is_string_literal(const CharT* p) noexcept {
  return __builtin_constant_p(::is_string_literal_helper(p));
}