Skip to content

Commit a19b2c5

Browse files
authored
api: Deprecate OIIO::string_view::c_str() (#4511)
Because it's not in std::string_view. If you must use this functionality (with caution about when it is safe), then use the freestanding `OIIO::c_str(OIIO::string_view)` function instead. I would like to include this warning in 3.0. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent 9f3e15f commit a19b2c5

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

docs/Deprecations-3.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ about being deprecated will be removed in the final 3.0 release.
206206
* The old OIIO_SIMD_HAS_FLOAT8 macro has been removed. It was deprecated since
207207
OIIO 1.8.
208208

209+
## string_view.h
210+
211+
* The string_view::c_str() method has been marked as deprecated, since
212+
it is not present in C++17 std::string_view. If you must use this
213+
functionality (with caution about when it is safe), then use the
214+
freestanding OIIO::c_str(string_view) function instead.
215+
209216
## strutil.h
210217

211218
* The default behavior of `Strutil::format()` has been changed to use the

src/include/OpenImageIO/string_view.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,10 @@ class basic_string_view {
139139
// N.B. std::string ctr from chars+len is constexpr in C++20.
140140
}
141141

142-
/// Explicitly request a 0-terminated string. USUALLY, this turns out to
143-
/// be just data(), with no significant added expense (because most uses
144-
/// of string_view are simple wrappers of C strings, C++ std::string, or
145-
/// ustring -- all of which are 0-terminated). But in the more rare case
146-
/// that the string_view represents a non-0-terminated substring, it
147-
/// will force an allocation and copy underneath.
148-
///
149-
/// Caveats:
150-
/// 1. This is NOT going to be part of the C++17 std::string_view, so
151-
/// it's probably best to avoid this method if you want to have 100%
152-
/// drop-in compatibility with std::string_view.
153-
/// 2. It is NOT SAFE to use c_str() on a string_view whose last char
154-
/// is the end of an allocation -- because that next char may only
155-
/// *coincidentally* be a '\0', which will cause c_str() to return
156-
/// the string start (thinking it's a valid C string, so why not just
157-
/// return its address?), if there's any chance that the subsequent
158-
/// char could change from 0 to non-zero during the use of the result
159-
/// of c_str(), and thus break the assumption that it's a valid C str.
142+
/// DEPRECATED(3.0) -- If you must use this at all, use the freestanding
143+
/// OIIO::c_str(OIIO::string_view). We want to get this out of the
144+
/// OIIO::string_view template to preserve symmetry with std::string_view.
145+
OIIO_DEPRECATED("Unsafe, nonstandard. Use standalone OIIO::c_str(string_view) if you must. (3.0)")
160146
const CharT* c_str() const;
161147

162148
/// Assignment
@@ -520,6 +506,7 @@ using wstring_view = basic_string_view<wchar_t>;
520506
OIIO_UTIL_API const char* c_str(string_view str);
521507

522508

509+
// DEPRECATED(3.0)
523510
template<> inline const char*
524511
basic_string_view<char>::c_str() const {
525512
return OIIO::c_str(*this);

0 commit comments

Comments
 (0)