зеркало из https://github.com/microsoft/cppwinrt.git
Structured binding support for IKeyValuePair (#590)
This commit is contained in:
Родитель
7b11196551
Коммит
ae94947a23
|
@ -66,3 +66,36 @@ WINRT_EXPORT namespace winrt
|
|||
return make<impl::observable_map<K, V, std::unordered_map<K, V, Hash, KeyEqual, Allocator>>>(std::move(values));
|
||||
}
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template<typename K, typename V>
|
||||
struct tuple_size<winrt::Windows::Foundation::Collections::IKeyValuePair<K, V>>
|
||||
: integral_constant<size_t, 2>
|
||||
{
|
||||
};
|
||||
|
||||
template<size_t Idx, typename K, typename V>
|
||||
struct tuple_element<Idx, winrt::Windows::Foundation::Collections::IKeyValuePair<K, V>>
|
||||
{
|
||||
static_assert(Idx < 2, "key-value pair index out of bounds");
|
||||
using type = conditional_t<Idx == 0, K, V>;
|
||||
};
|
||||
}
|
||||
|
||||
namespace winrt::Windows::Foundation::Collections
|
||||
{
|
||||
template<size_t Idx, typename K, typename V>
|
||||
std::tuple_element_t<Idx, IKeyValuePair<K, V>> get(IKeyValuePair<K, V> const& kvp)
|
||||
{
|
||||
static_assert(Idx < 2, "key-value pair index out of bounds");
|
||||
if constexpr (Idx == 0)
|
||||
{
|
||||
return kvp.Key();
|
||||
}
|
||||
else
|
||||
{
|
||||
return kvp.Value();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,6 +69,21 @@ TEST_CASE("range_for,IIterable,IKeyValuePair")
|
|||
for (IKeyValuePair<int32_t, hstring> i : c)
|
||||
{
|
||||
result[i.Key()] = i.Value();
|
||||
|
||||
// Sneak in a structured binding test.
|
||||
auto const [key, value] = i;
|
||||
REQUIRE(key == i.Key());
|
||||
REQUIRE(value == i.Value());
|
||||
}
|
||||
|
||||
REQUIRE(result == values);
|
||||
|
||||
// Do it again with structured bindings.
|
||||
result.clear();
|
||||
|
||||
for (auto&& [key, value] : c)
|
||||
{
|
||||
result[key] = value;
|
||||
}
|
||||
|
||||
REQUIRE(result == values);
|
||||
|
@ -92,6 +107,16 @@ TEST_CASE("range_for,IMap")
|
|||
}
|
||||
|
||||
REQUIRE(result == values);
|
||||
|
||||
// Do it again with structured bindings.
|
||||
result.clear();
|
||||
|
||||
for (auto&& [key, value] : c)
|
||||
{
|
||||
result[key] = value;
|
||||
}
|
||||
|
||||
REQUIRE(result == values);
|
||||
}
|
||||
|
||||
TEST_CASE("range_for,IMapView")
|
||||
|
@ -112,5 +137,15 @@ TEST_CASE("range_for,IMapView")
|
|||
}
|
||||
|
||||
REQUIRE(result == values);
|
||||
|
||||
// Do it again with structured bindings.
|
||||
result.clear();
|
||||
|
||||
for (auto&& [key, value] : c)
|
||||
{
|
||||
result[key] = value;
|
||||
}
|
||||
|
||||
REQUIRE(result == values);
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче