Skip to content

Provide type trait or concept to verify if a projection type derives from another #609

@sylveon

Description

@sylveon

It might be useful in cases where a template uses a C++/WinRT projection type, to constrain that template to only types deriving from another projection type.

This can currently be achieved multiple ways:

template<typename T>
	requires std::derived_from<T, winrt::impl::base_one<T, winrt::Windows::UI::Xaml::Controls::Page>>
class XamlPageHost final : public BaseXamlPageHost {
template<typename T>
	requires std::convertible_to<T, winrt::Windows::UI::Xaml::Controls::Page>
class XamlPageHost final : public BaseXamlPageHost {

The problem with solution 1 is that it taps into implementation details of C++/WinRT, which is undesirable for a consumer of the library, but it is a reliable way currently to detect if T derives from Page.
Solution 2 has the issue that if some other non-projection type offers a conversion operator to the desired projection type, the template will match.

To resolve this situation, there should be a winrt::is_derived_from<T, U> type trait (and winrt::is_derived_from_v shorthand) which would have a static constexpr member value that is true, if the projection type T derives from type U.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions