public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* usage of __PRETTY_FUNCTION__ in constexpr
@ 2023-09-19  8:52 Fabian H
  0 siblings, 0 replies; only message in thread
From: Fabian H @ 2023-09-19  8:52 UTC (permalink / raw)
  To: gcc-help

Hi,

I hope this is the correct place to ask such questions.

I'm using a library that uses __PRETTY_FUNCTION__ in a constexpr to
determine the name of a type at compile time.

Which pretty much looks like this:

template<typename Type>
[[nodiscard]] constexpr auto stripped_type_name() noexcept
{
  std::string_view pretty_function{
    __PRETTY_FUNCTION__
  }; // Results in "constexpr auto stripped_type_name() [with Type =
std::vector<entt::entity>]"
  auto first = pretty_function.find_first_not_of(' ',
pretty_function.find_first_of('=') + 1);
  auto value = pretty_function.substr(first,
pretty_function.find_last_of(']') - first);
  return value;
}

template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
[[nodiscard]] static constexpr std::string_view type_name(int) noexcept
{
  constexpr auto value = stripped_type_name<Type>();
  return value;
}

For me that results in flaky behaviour depending on the compilation
unit, optimization level and various unrelated code changes.
It either returns

"std::vector<entt::entity, std::allocator<entt::entity> >"
or
"std::vector<entt::entity>"

when calling
stripped_type_name<std::vector<entt::entity>>()

I was wondering if this is a proper use of __PRETTY_FUNCTION__ and/or
what could explain such behaviour.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-09-19  8:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-19  8:52 usage of __PRETTY_FUNCTION__ in constexpr Fabian H

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).