Skip to content

Commit d7c89b5

Browse files
authored
Temporary std::pair sometimes causes spurious temporary dtor calls (#1462)
1 parent cf96df5 commit d7c89b5

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

cppwinrt/code_writers.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,8 @@ namespace cppwinrt
11371137
{%
11381138
if constexpr (!std::is_same_v<D, %>)
11391139
{
1140-
auto const [_winrt_casted_result, _winrt_cast_result_code] = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this));
1140+
winrt::hresult _winrt_cast_result_code;
1141+
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
11411142
check_hresult(_winrt_cast_result_code);
11421143
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
11431144
_winrt_abi_type->%(%);
@@ -1156,7 +1157,8 @@ namespace cppwinrt
11561157
{%
11571158
if constexpr (!std::is_same_v<D, %>)
11581159
{
1159-
auto const [_winrt_casted_result, _winrt_cast_result_code] = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this));
1160+
winrt::hresult _winrt_cast_result_code;
1161+
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
11601162
check_hresult(_winrt_cast_result_code);
11611163
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
11621164
WINRT_VERIFY_(0, _winrt_abi_type->%(%));
@@ -1176,7 +1178,8 @@ namespace cppwinrt
11761178
{%
11771179
if constexpr (!std::is_same_v<D, %>)
11781180
{
1179-
auto const [_winrt_casted_result, _winrt_cast_result_code] = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this));
1181+
winrt::hresult _winrt_cast_result_code;
1182+
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
11801183
check_hresult(_winrt_cast_result_code);
11811184
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
11821185
check_hresult(_winrt_abi_type->%(%));

strings/base_implements.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,17 +794,17 @@ namespace winrt::impl
794794
}
795795

796796
template <typename To, typename From>
797-
friend auto winrt::impl::try_as_with_reason(From ptr) noexcept;
797+
friend auto winrt::impl::try_as_with_reason(From ptr, hresult& code) noexcept;
798798

799799
protected:
800800
static constexpr bool is_composing = true;
801801
Windows::Foundation::IInspectable m_inner;
802802

803803
private:
804804
template <typename Qi>
805-
auto try_as_with_reason() const noexcept
805+
auto try_as_with_reason(hresult& code) const noexcept
806806
{
807-
return m_inner.try_as_with_reason<Qi>();
807+
return m_inner.try_as_with_reason<Qi>(code);
808808
}
809809
};
810810

strings/base_windows.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,27 @@ namespace winrt::impl
133133
}
134134

135135
template <typename To, typename From, std::enable_if_t<is_com_interface_v<To>, int> = 0>
136-
std::pair<com_ref<To>, hresult> try_as_with_reason(From* ptr) noexcept
136+
com_ref<To> try_as_with_reason(From* ptr, hresult& code) noexcept
137137
{
138138
#ifdef WINRT_DIAGNOSTICS
139139
get_diagnostics_info().add_query<To>();
140140
#endif
141141

142142
if (!ptr)
143143
{
144-
return { nullptr, 0 };
144+
code = 0;
145+
return nullptr;
145146
}
146147

147148
void* result{};
148-
hresult code = ptr->QueryInterface(guid_of<To>(), &result);
149-
return { wrap_as_result<To>(result), code };
149+
code = ptr->QueryInterface(guid_of<To>(), &result);
150+
return wrap_as_result<To>(result);
150151
}
151152

152153
template <typename To, typename From>
153-
auto try_as_with_reason(From ptr) noexcept
154+
auto try_as_with_reason(From ptr, hresult& code) noexcept
154155
{
155-
return ptr->template try_as_with_reason<To>();
156+
return ptr->template try_as_with_reason<To>(code);
156157
}
157158
}
158159

@@ -229,9 +230,9 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
229230
}
230231

231232
template <typename To>
232-
auto try_as_with_reason() const noexcept
233+
auto try_as_with_reason(hresult& code) const noexcept
233234
{
234-
return impl::try_as_with_reason<To>(m_ptr);
235+
return impl::try_as_with_reason<To>(m_ptr, code);
235236
}
236237

237238
template <typename To>

0 commit comments

Comments
 (0)