Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:46:11+00:00 2026-05-23T16:46:11+00:00

If I have two variadic template arguments, A and B , how can I

  • 0

If I have two variadic template arguments, A and B, how can I ensure at compile-time that the types of all of the members of A are also the types of a subset of B (in the same order)?

Contrived example:

template<typename...A>
struct Foo {
  template<typename...B>
  static void bar()
  {
  }
}

...

Foo<Apple, Orange>:: template bar<Apple, Orange, Grape>(); // this compiles
Foo<Apple, Orange>:: template bar<Orange, Grape>(); // this doesn't
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:46:12+00:00Added an answer on May 23, 2026 at 4:46 pm

    For a general subset I don’t know, but if you can guarantee that B is of the form A..., More..., then this may do:

    #include <functional>
    #include <tuple>
    
    template <typename ...A>
    struct var_equal : std::false_type { };
    
    template <typename A1, typename ...Aother, typename B1, typename ...Bother>
    struct var_equal<std::tuple<A1, Aother...>, std::tuple<B1, Bother...>>
    {
      static const bool value = std::is_same<A1, B1>::value && var_equal<std::tuple<Aother...>, std::tuple<Bother...>>::value;
    };
    
    template <typename ...B>
    struct var_equal<std::tuple<>, std::tuple<B...>> : std::true_type { };
    
    
    template<typename...A>
    struct Foo {
      template<typename...B>
      static void bar()
      {
        static_assert(var_equal<std::tuple<A...>, std::tuple<B...>>::value, "Hello");
      }
    };
    

    (Sorry, var_equal is a terrible name. It should be called something more appropriate, like initial_equal.)


    Update: Here is the general solution, worked out in detail by Luc Danton (see here for his beautifully styled code):

    #include <type_traits>
    #include <tuple>
    
    template <typename Sub, typename Super>
    struct subset_of : std::false_type {};
    
    template<typename Same, typename... AOther, typename... BOther>
    struct subset_of<std::tuple<Same, AOther...>, std::tuple<Same, BOther...>>
    : subset_of<
        std::tuple<AOther...>
        , std::tuple<BOther...>
    > {};
    
    template<typename ADifferent, typename BDifferent, typename... AOther, typename... BOther>
    struct subset_of<std::tuple<ADifferent, AOther...>, std::tuple<BDifferent, BOther...>>
    : subset_of<
        std::tuple<ADifferent, AOther...>
        , std::tuple<BOther...>
    > {};
    
    template<typename... B>
    struct subset_of<std::tuple<>, std::tuple<B...>>: std::true_type {};
    
    template<typename... A>
    struct Foo {
        template<typename... B>
        static void bar()
        {
            static_assert(subset_of<std::tuple<A...>, std::tuple<B...>>::value, "Hello");
        }
    };
    

    Test case:

    struct Apple{}; struct Orange{}; struct Grape{};
    
    int main()
    {
        Foo<Apple, Orange>::bar<Apple, Orange, Grape>();               // this compiles
        Foo<Apple, Orange>::bar<Grape, Apple, Grape, Orange, Grape>(); // this also compiles
        Foo<Apple, Orange>::bar<Orange, Grape>();                      // this doesn't
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C++0x thread library or Boost.thread define a non-member variadic template function that locks all
I have two images they actually do not overlap but I also can't have
I have two applications written in Java that communicate with each other using XML
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
I have two date fields that I need to join on. The first is
I have two BizTalk 2010 servers that belong to the same BizTalk group. They
I have two loggers. One that appends to a file, and another that is
I have two function a() and b(), both are variadic functions, let say when
Assume I have encoded the natural numbers in Haskell types, and that I have
I have two DataGridViews that have the same column schema (although two different DataViews

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.