Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use typeid() in std.h when it is available #3468

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ class format_arg_store
basic_format_args<Context>(*this),
#endif
data_{detail::make_arg<is_packed, Context>(args)...} {
if (num_named_args != 0)
if (detail::const_check(num_named_args != 0))
vitaut marked this conversation as resolved.
Show resolved Hide resolved
detail::init_named_args(data_.named_args(), 0, 0, args...);
}
};
Expand Down
30 changes: 25 additions & 5 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@
# endif
#endif

// Check if typeid is available.
#ifndef FMT_USE_TYPEID
// In MSVC, typeid() is available with or without RTTI.
# if defined(_MSC_VER)
# define FMT_USE_TYPEID 1
# elif defined(__RTTI) // EDG compilers.
# define FMT_USE_TYPEID 1
# elif defined(__INTEL_RTTI__) // Intel compiler.
# define FMT_USE_TYPEID 1
# elif defined(__GXX_RTTI) // G++.
# define FMT_USE_TYPEID 1
# elif FMT_HAS_FEATURE(cxx_rtti) // Clang.
# define FMT_USE_TYPEID 1
# else
# define FMT_USE_TYPEID 0
# endif
#endif

#ifdef __cpp_lib_filesystem
FMT_BEGIN_NAMESPACE

Expand Down Expand Up @@ -274,7 +292,7 @@ struct formatter<
if (it == end || *it == '}') return it;
if (*it == 't') {
++it;
with_typename_ = true;
with_typename_ = FMT_USE_TYPEID != 0;
}
return it;
}
Expand All @@ -287,8 +305,9 @@ struct formatter<
if (!with_typename_)
return detail::write_bytes(out, string_view(ex.what()), spec);

#if FMT_USE_TYPEID
const std::type_info& ti = typeid(ex);
#ifdef FMT_HAS_ABI_CXA_DEMANGLE
# ifdef FMT_HAS_ABI_CXA_DEMANGLE
int status = 0;
std::size_t size = 0;
std::unique_ptr<char, decltype(&std::free)> demangled_name_ptr(
Expand Down Expand Up @@ -327,21 +346,22 @@ struct formatter<
demangled_name_view = string_view(ti.name());
}
out = detail::write_bytes(out, demangled_name_view, spec);
#elif FMT_MSC_VERSION
# elif FMT_MSC_VERSION
string_view demangled_name_view(ti.name());
if (demangled_name_view.starts_with("class "))
demangled_name_view.remove_prefix(6);
else if (demangled_name_view.starts_with("struct "))
demangled_name_view.remove_prefix(7);
out = detail::write_bytes(out, demangled_name_view, spec);
#else
# else
out = detail::write_bytes(out, string_view(ti.name()), spec);
#endif
# endif
out = detail::write<Char>(out, Char(':'));
out = detail::write<Char>(out, Char(' '));
out = detail::write_bytes(out, string_view(ex.what()), spec);

return out;
#endif
}
};
FMT_END_NAMESPACE
Expand Down