CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Variadic templates and type composition

  1. #1
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Variadic templates and type composition

    Here's an interesting question I encountered. This isn't critical, but it would be "cool" to know how to accomplish it....

    Let's say I have a variadic type A and a non-variadic type B:
    Code:
    template <typename ... Args>
    class A
    {};
    
    template <typename T>
    class B
    {};
    Now, I'd like to do the following (pseudocode since I don't know how to do this yet):
    Code:
    template <typename ... Args>
    class C: public A<B<Args1>,B<Args2>,B<Args3>....>
    {};
    Essentially, I'd like to take the parameter pack used for C and compose B around each element individually, then pass the result to A.

    Any idea how I can do this?

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Variadic templates and type composition

    Code:
    template <typename... Args>
    class C: public A<B<Args>...>
    {};
    when appearing where a parameter pack can be used ( and in other contexts too ), packs expansions ( eg, an expression E followed by an "..." ) will expand into a parameter pack E1,E2,.. where Ei is the expression E where any parameter pack "...P" appearing in it is replaced with its ith element Pi.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Variadic templates and type composition

    Thanks, that's exactly what I needed!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)