Skip to content

Commit 432bc5c

Browse files
authored
Add std::string clean_type_id(const char *typeid_name) overload (in namespace detail). (#4049)
Very minor refactoring to ease development and debugging. Having to declare a local `std::string` has bugged me many times. Nice to get this little nuisance out of the way. Extracted from PR #4022, where it is used like this: ``` std::fprintf(stdout, "\nTYPE_CASTER_ODR_GUARD_IMPL %s %s\n", clean_type_id(intrinsic_type_info.name()).c_str(), source_file_line_from_sloc.c_str()); ```
1 parent 85bc088 commit 432bc5c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

include/pybind11/detail/typeid.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
2222
PYBIND11_NAMESPACE_BEGIN(detail)
23+
2324
/// Erase all occurrences of a substring
2425
inline void erase_all(std::string &string, const std::string &search) {
2526
for (size_t pos = 0;;) {
@@ -46,14 +47,19 @@ PYBIND11_NOINLINE void clean_type_id(std::string &name) {
4647
#endif
4748
detail::erase_all(name, "pybind11::");
4849
}
50+
51+
inline std::string clean_type_id(const char *typeid_name) {
52+
std::string name(typeid_name);
53+
detail::clean_type_id(name);
54+
return name;
55+
}
56+
4957
PYBIND11_NAMESPACE_END(detail)
5058

5159
/// Return a string representation of a C++ type
5260
template <typename T>
5361
static std::string type_id() {
54-
std::string name(typeid(T).name());
55-
detail::clean_type_id(name);
56-
return name;
62+
return detail::clean_type_id(typeid(T).name());
5763
}
5864

5965
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

0 commit comments

Comments
 (0)