diff --git a/pybind11_abseil/display_source_location_in_python.h b/pybind11_abseil/display_source_location_in_python.h index 01a994a..e68c246 100644 --- a/pybind11_abseil/display_source_location_in_python.h +++ b/pybind11_abseil/display_source_location_in_python.h @@ -4,6 +4,8 @@ // This file exists to simplify adding C++ source location to python StatusNotOk // traces. See: b/266066084 for details. +#include + #include "absl/status/status.h" #include "util/task/status_builder.h" @@ -23,24 +25,26 @@ util::StatusBuilder DisplaySourceLocationInPython(util::StatusBuilder sb); util::StatusBuilder DoNotDisplaySourceLocationInPython(util::StatusBuilder sb); // Annotate the StatusOr to display the c++ SourceLocation in python. -template -StatusOrT DisplaySourceLocationInPython(StatusOrT&& s_or_t) { +// The argument is a reference type and the return value is a value type. +// This avoids returning a reference to a stack variable. +template +std::decay_t DisplaySourceLocationInPython(StatusOrT&& s_or_t) { if (s_or_t.ok()) { return s_or_t; } absl::Status status_with_payload = DisplaySourceLocationInPython( s_or_t.status()); - return StatusOrT{status_with_payload}; + return std::decay_t{status_with_payload}; } -template -StatusOrT DoNotDisplaySourceLocationInPython(StatusOrT&& s_or_t) { +template +std::decay_t DoNotDisplaySourceLocationInPython(StatusOrT&& s_or_t) { if (s_or_t.ok()) { return s_or_t; } absl::Status status_with_payload = DoNotDisplaySourceLocationInPython( s_or_t.status()); - return StatusOrT{status_with_payload}; + return std::decay_t{status_with_payload}; } } // namespace google