From: Simon Marchi Date: Tue, 16 Jan 2024 18:58:25 +0000 (-0500) Subject: cpp-common/bt2c: make CStringView convertible from std::string X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=a0ae532f4ebf0a5d31f8378fd0584d420e9eca7e cpp-common/bt2c: make CStringView convertible from std::string This will make it possible to have functions that accept CStringViews: void foo(CStringView); and pass in std::strings seamlessly: foo(myStdStr); Change-Id: I76dc55438c2cd87f77ddec421dff7f119e53eff5 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/11688 Reviewed-by: Philippe Proulx --- diff --git a/src/cpp-common/bt2c/c-string-view.hpp b/src/cpp-common/bt2c/c-string-view.hpp index c1c8bf38..fb1b3a65 100644 --- a/src/cpp-common/bt2c/c-string-view.hpp +++ b/src/cpp-common/bt2c/c-string-view.hpp @@ -42,6 +42,15 @@ public: { } + /* + * Builds a view of the string `str`. + * + * Intentionally not explicit. + */ + CStringView(const std::string& str) noexcept : _mStr {str.c_str()} + { + } + /* * Makes this view view the C string `str` (may be `nullptr`). *