Skip to content

Commit f631b09

Browse files
authored
Replace STF type_name implementation with cuda::std::__pretty_nameof wrapper (#8194)
Remove the home-grown __PRETTY_FUNCTION__ parsing in traits.cuh and replace type_name<T> with a thin wrapper around cuda::std::__pretty_nameof<T>(), which is the existing robust CCCL facility for compile-time type name extraction. Made-with: Cursor
1 parent ea4eb21 commit f631b09

File tree

1 file changed

+5
-47
lines changed
  • cudax/include/cuda/experimental/__stf/utility

1 file changed

+5
-47
lines changed

cudax/include/cuda/experimental/__stf/utility/traits.cuh

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif // no system header
2727

2828
#include <cuda/std/__utility/exception_guard.h>
29+
#include <cuda/std/__utility/typeid.h>
2930
#include <cuda/std/mdspan>
3031

3132
#include <cuda/experimental/__stf/utility/core.cuh>
@@ -37,62 +38,19 @@
3738

3839
namespace cuda::experimental::stf
3940
{
40-
namespace reserved
41-
{
42-
// We use this function as a detector for what __PRETTY_FUNCTION__ looks like
43-
template <typename T>
44-
constexpr ::std::string_view type_name_IMPL()
45-
{
46-
#if _CCCL_COMPILER(MSVC)
47-
return __FUNCSIG__;
48-
#else // ^^^ _CCCL_COMPILER(MSVC) ^^^ / vvv !_CCCL_COMPILER(MSVC) vvv
49-
return __PRETTY_FUNCTION__;
50-
#endif // !_CCCL_COMPILER(MSVC)
51-
}
52-
53-
// Length of prefix and suffix in __PRETTY_FUNCTION__ when used with `type_name`.
54-
inline constexpr ::std::pair<size_t, size_t> type_name_affixes = [] {
55-
const auto p = type_name_IMPL<double>();
56-
const auto target = ::std::string_view("double");
57-
const auto len = target.size();
58-
// Simulate p.find() by hand because clang can't do it.
59-
size_t i = target.npos;
60-
for (std::size_t start = 0; start <= p.size() - len; ++start)
61-
{
62-
if (p.substr(start, len) == target)
63-
{
64-
i = start; // Found the substring, set i to the starting position
65-
break; // Exit loop after finding the first match
66-
}
67-
}
68-
auto j = p.size() - i - len;
69-
return ::std::pair{i, j};
70-
}();
71-
72-
template <class T>
73-
constexpr ::std::string_view type_name_impl()
74-
{
75-
#if _CCCL_COMPILER(MSVC)
76-
constexpr ::std::string_view p = __FUNCSIG__;
77-
// MSVC does not provide constexpr methods so we make this utility much simpler and return __FUNCSIG__ directly
78-
return p;
79-
#else // ^^^ _CCCL_COMPILER(MSVC) ^^^ / vvv !_CCCL_COMPILER(MSVC) vvv
80-
::std::string_view p = __PRETTY_FUNCTION__;
81-
return p.substr(type_name_affixes.first, p.size() - type_name_affixes.first - type_name_affixes.second);
82-
#endif // !_CCCL_COMPILER(MSVC)
83-
}
84-
} // namespace reserved
85-
8641
/**
8742
* @brief Yields a string form of type name. Exact spelling not guaranteed (e.g. `type_name<int*>` may be `"int*"`,
8843
* `"int *"` etc).
8944
*
45+
* Thin wrapper around `cuda::std::__pretty_nameof<T>()` returning `::std::string_view`.
46+
*
9047
* @tparam T The type to show.
9148
*
9249
* @snippet unittest.h type_name
9350
*/
9451
template <class T>
95-
inline constexpr ::std::string_view type_name = reserved::type_name_impl<T>();
52+
inline const ::std::string_view type_name{
53+
::cuda::std::__pretty_nameof<T>().data(), ::cuda::std::__pretty_nameof<T>().size()};
9654

9755
/**
9856
* @brief Converts each element in `t` to a new value by calling `f`, then returns a tuple collecting the values thus

0 commit comments

Comments
 (0)