consume_t template

Rudolf Meier 291 Reputation points
2022-03-16T22:02:28.04+00:00

what's the purpose of the consume_t template class? I don't understand how it works

that's the first things I found and copied out...

template <typename T>
struct consume;

template <typename D>
struct consume_IInterface
{
    [[nodiscard]] WINRT_IMPL_AUTO(hstring) Verb() const;
};

template <> struct consume<IInterface>
{
    template <typename D> using type = consume_IInterface<D>;
};

template <typename D, typename I = D>
using consume_t = typename consume<I>::template type<D>;

template <typename D, typename I>
struct require_one : consume_t<D, I>

template <typename D, typename... I>
struct __declspec(empty_bases) require : require_one<D, I>...
{};

...
now, I see multiple classes defined as

struct something : require<something, IInterface>
{}

so, let's analyze it... first, require does derive from every passed interface by using require_one... that's the easy part. Then consume_t is used and here I don't see what the purpose is... it's inheriting from a inner template class of the "consume<I>" template class? ... why? what's the purpose of it? ... I didn't find the corresponding code that explains this.
And then... what's the purpose of the finally inherited class? Does it do anything? What's this "Verb" function doing they added?

Developer technologies | C++
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2022-03-17T03:14:34.82+00:00

    Hi @Rudolf Meier ,

    About alias templates:

    template < template-parameter-list >  
    using identifier attr(optional) = type-id ;  
    

    attr - optional sequence of any number of attributes
    identifier - the name that is introduced by this declaration, which becomes either a type name or a template name.
    template-parameter-list - template parameter list, as in template declaration.
    type-id - abstract declarator or any other valid type-id (which may introduce a new type, as noted in type-id). The type-id cannot directly or indirectly refer to identifier. Note that the point of declaration of the identifier is at the semicolon following type-id.

    About Inheritance I suggest you to read this document.

     template <typename D, typename I>  
     struct require_one : consume_t<D, I>  
    

    In my opinion, the code you uploaded should come from a tutorial, the point of this code is to show you inheritance between class templates. You may be confused due to lack of application of class templates.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.