Skip to content

Commit

Permalink
Utility: make IsStringLike recognize C++17 std::string_view.
Browse files Browse the repository at this point in the history
Debug used to print it as a numeric container, which isn't really
wanted. It can be thought of as a breaking change however, and so it's
listed as such.
  • Loading branch information
mosra committed Feb 19, 2022
1 parent e7ec9c5 commit 46f74dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 6 additions & 0 deletions doc/corrade-changelog.dox
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ namespace Corrade {
@ref Containers::String using @ref Utility::format()
- Creating an empty path with @ref Utility::Directory::mkpath() now succeeds
because it makes no sense to fail for such case
- @ref Utility::IsStringLike now recognizes also C++17 @ref std::string_view
by checking for a @cpp substr() @ce member instead of @cpp c_str() @ce
- The @ref CORRADE_LONG_DOUBLE_SAME_AS_DOUBLE macro is now defined on
Emscripten always, because even if it's @cpp long double @ce is 80-bit
sometimes, it doesn't actually have the precision of a 80-bit
Expand Down Expand Up @@ -373,6 +375,10 @@ namespace Corrade {
@ref Containers::MutableStringView instead of @ref Containers::ArrayView.
The types are however implicitly convertible and thus breakages are not
expected in majority of existing code.
- Printing a C++17 @ref std::string_view with @ref Utility::Debug now
requires the @ref Corrade/Utility/DebugStlStringView.h include. Before it
was printed as a numeric container due to @ref Utility::IsStringLike not
recognizing it.

@subsection corrade-changelog-latest-documentation Documentation

Expand Down
10 changes: 5 additions & 5 deletions src/Corrade/Utility/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace Implementation {
CORRADE_HAS_TYPE(HasMemberEnd, decltype(std::declval<T>().end()));
CORRADE_HAS_TYPE(HasBegin, decltype(begin(std::declval<T>())));
CORRADE_HAS_TYPE(HasEnd, decltype(end(std::declval<T>())));
CORRADE_HAS_TYPE(HasMemberCStr, decltype(std::declval<T>().c_str()));
CORRADE_HAS_TYPE(HasMemberSubstr, decltype(std::declval<T>().substr()));
}

/**
Expand Down Expand Up @@ -221,19 +221,19 @@ template<class T> using IsIterable = std::integral_constant<bool,
@brief Traits class for checking whether given type is string-like
@m_since{2019,10}
Equivalent to @ref std::true_type if the class is has a @cpp c_str() @ce
Equivalent to @ref std::true_type if the class is has a @cpp substr() @ce
member or is a @ref Containers::BasicStringView "Containers::[Mutable]StringView"
/ @ref Containers::String. Otherwise equivalent to @ref std::false_type. Useful
for dispatching on the @ref std::string type without having to include or
@ref StlForwardString.h "forward-declare" it.
for dispatching on the @ref std::string or the C++17 @ref std::string_view type
without having to include or @ref StlForwardString.h "forward-declare" them.
Used together with @ref IsIterable by @ref Debug to decide whether given type
should be printed as a container of its contents or as a whole.
@todoc use the ellipsis macro once m.css has it
*/
template<class T> using IsStringLike = std::integral_constant<bool,
#ifndef DOXYGEN_GENERATING_OUTPUT
Implementation::HasMemberCStr<T>::value || std::is_same<typename std::decay<T>::type, Containers::StringView>::value || std::is_same<typename std::decay<T>::type, Containers::MutableStringView>::value || std::is_same<typename std::decay<T>::type, Containers::String>::value
Implementation::HasMemberSubstr<T>::value || std::is_same<typename std::decay<T>::type, Containers::StringView>::value || std::is_same<typename std::decay<T>::type, Containers::MutableStringView>::value || std::is_same<typename std::decay<T>::type, Containers::String>::value
#else
implementation-specific
#endif
Expand Down

0 comments on commit 46f74dc

Please sign in to comment.