microsoft / wil

Windows Implementation Library
MIT License
2.57k stars 234 forks source link

Add a wil::zwstring_view ctor that accepts a wil::hstring #451

Closed jonwis closed 1 month ago

jonwis commented 1 month ago

So like this:

#include <string_view>
#include <algorithm>

template<typename Q> struct is_null_terminated_counted : std::false_type{};

template <class TChar>
class basic_zstring_view : public std::basic_string_view<TChar>
{
public:
    template<typename Q>
    basic_zstring_view(Q const& src, std::enable_if_t<is_null_terminated_counted<Q>::value>* = nullptr) noexcept : 
        std::basic_string_view<TChar>(src.data(), src.size()) {}
};

template<> struct is_null_terminated_counted<winrt::hstring> : std::true_type{};

auto foo(muffin const& r) {
    return basic_zstring_view<wchar_t>{r};
}
jonwis commented 1 month ago

Nevermind, this is #363