Skip to content

Commit

Permalink
Temporary std::pair sometimes causes spurious temporary dtor calls (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DefaultRyan authored Dec 12, 2024
1 parent cf96df5 commit d7c89b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
9 changes: 6 additions & 3 deletions cppwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@ namespace cppwinrt
{%
if constexpr (!std::is_same_v<D, %>)
{
auto const [_winrt_casted_result, _winrt_cast_result_code] = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this));
winrt::hresult _winrt_cast_result_code;
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
check_hresult(_winrt_cast_result_code);
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
_winrt_abi_type->%(%);
Expand All @@ -1156,7 +1157,8 @@ namespace cppwinrt
{%
if constexpr (!std::is_same_v<D, %>)
{
auto const [_winrt_casted_result, _winrt_cast_result_code] = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this));
winrt::hresult _winrt_cast_result_code;
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
check_hresult(_winrt_cast_result_code);
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
WINRT_VERIFY_(0, _winrt_abi_type->%(%));
Expand All @@ -1176,7 +1178,8 @@ namespace cppwinrt
{%
if constexpr (!std::is_same_v<D, %>)
{
auto const [_winrt_casted_result, _winrt_cast_result_code] = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this));
winrt::hresult _winrt_cast_result_code;
auto const _winrt_casted_result = impl::try_as_with_reason<%, D const*>(static_cast<D const*>(this), _winrt_cast_result_code);
check_hresult(_winrt_cast_result_code);
auto const _winrt_abi_type = *(abi_t<%>**)&_winrt_casted_result;
check_hresult(_winrt_abi_type->%(%));
Expand Down
6 changes: 3 additions & 3 deletions strings/base_implements.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,17 +794,17 @@ namespace winrt::impl
}

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

protected:
static constexpr bool is_composing = true;
Windows::Foundation::IInspectable m_inner;

private:
template <typename Qi>
auto try_as_with_reason() const noexcept
auto try_as_with_reason(hresult& code) const noexcept
{
return m_inner.try_as_with_reason<Qi>();
return m_inner.try_as_with_reason<Qi>(code);
}
};

Expand Down
17 changes: 9 additions & 8 deletions strings/base_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,27 @@ namespace winrt::impl
}

template <typename To, typename From, std::enable_if_t<is_com_interface_v<To>, int> = 0>
std::pair<com_ref<To>, hresult> try_as_with_reason(From* ptr) noexcept
com_ref<To> try_as_with_reason(From* ptr, hresult& code) noexcept
{
#ifdef WINRT_DIAGNOSTICS
get_diagnostics_info().add_query<To>();
#endif

if (!ptr)
{
return { nullptr, 0 };
code = 0;
return nullptr;
}

void* result{};
hresult code = ptr->QueryInterface(guid_of<To>(), &result);
return { wrap_as_result<To>(result), code };
code = ptr->QueryInterface(guid_of<To>(), &result);
return wrap_as_result<To>(result);
}

template <typename To, typename From>
auto try_as_with_reason(From ptr) noexcept
auto try_as_with_reason(From ptr, hresult& code) noexcept
{
return ptr->template try_as_with_reason<To>();
return ptr->template try_as_with_reason<To>(code);
}
}

Expand Down Expand Up @@ -229,9 +230,9 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
}

template <typename To>
auto try_as_with_reason() const noexcept
auto try_as_with_reason(hresult& code) const noexcept
{
return impl::try_as_with_reason<To>(m_ptr);
return impl::try_as_with_reason<To>(m_ptr, code);
}

template <typename To>
Expand Down

0 comments on commit d7c89b5

Please sign in to comment.