Skip to content

Commit 080e6a9

Browse files
pybind11_abseil authorscopybara-github
authored andcommitted
Internal change.
PiperOrigin-RevId: 893515854
1 parent c001d3f commit 080e6a9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pybind11_abseil/display_source_location_in_python.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// This file exists to simplify adding C++ source location to python StatusNotOk
55
// traces. See: b/266066084 for details.
66

7+
#include <type_traits>
8+
79
#include "absl/status/status.h"
810
#include "util/task/status_builder.h"
911

@@ -23,24 +25,26 @@ util::StatusBuilder DisplaySourceLocationInPython(util::StatusBuilder sb);
2325
util::StatusBuilder DoNotDisplaySourceLocationInPython(util::StatusBuilder sb);
2426

2527
// Annotate the StatusOr<T> to display the c++ SourceLocation in python.
28+
// The argument is a reference type and the return value is a value type.
29+
// This avoids returning a reference to a stack variable.
2630
template<typename StatusOrT>
27-
StatusOrT DisplaySourceLocationInPython(StatusOrT&& s_or_t) {
31+
std::decay_t<StatusOrT> DisplaySourceLocationInPython(StatusOrT&& s_or_t) {
2832
if (s_or_t.ok()) {
2933
return s_or_t;
3034
}
3135
absl::Status status_with_payload = DisplaySourceLocationInPython(
3236
s_or_t.status());
33-
return StatusOrT{status_with_payload};
37+
return std::decay_t<StatusOrT>{status_with_payload};
3438
}
3539

3640
template<typename StatusOrT>
37-
StatusOrT DoNotDisplaySourceLocationInPython(StatusOrT&& s_or_t) {
41+
std::decay_t<StatusOrT> DoNotDisplaySourceLocationInPython(StatusOrT&& s_or_t) {
3842
if (s_or_t.ok()) {
3943
return s_or_t;
4044
}
4145
absl::Status status_with_payload = DoNotDisplaySourceLocationInPython(
4246
s_or_t.status());
43-
return StatusOrT{status_with_payload};
47+
return std::decay_t<StatusOrT>{status_with_payload};
4448
}
4549

4650
} // namespace google

0 commit comments

Comments
 (0)