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 671657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:23:53+00:00 2026-05-14T00:23:53+00:00

Say you have a tuple and want to generate a new tuple by applying

  • 0

Say you have a tuple and want to generate a new tuple by applying a metafunction on each type of the first one. What’ the most efficient C++ metafuntion to accomplish to this task? Is it also possible to use C++0x variadic template to provide a better implementation?

  • 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-14T00:23:53+00:00Added an answer on May 14, 2026 at 12:23 am

    How ’bout this one:

    template<typename Metafun, typename Tuple>
    struct mod;
    
    // using a meta-function class
    template<typename Metafun, template<typename...> class Tuple, typename ...Types>
    struct mod<Metafun, Tuple<Types...>> {
      typedef Tuple<typename Metafun::template apply<Types>::type...>
        type;
    };
    

    Then

    typedef std::tuple<int, bool> tuple_foo;
    
    struct add_pointer {
      template<typename T>
      struct apply { typedef T *type; };
    };
    
    typedef mod<add_pointer, tuple_foo>::type tuple_ptrfoo;
    

    That’s using a metafunction class by wrapping the apply into a non-template. That allows passing it to C++03 templates (which cannot accept templates with arbitrary parameters by simply doing template<typename...> class X). You may, of course, accept a pure metafunction (not a class) too

    template<template<typename...> class Metafun, typename Tuple>
    struct mod;
    
    // using a meta-function
    template<template<typename...> class Metafun, template<typename...> class Tuple, 
             typename ...Types>
    struct mod<Metafun, Tuple<Types...>> {
      typedef Tuple<typename Metafun<Types>::type...>
        type;
    };
    

    And use the std::add_pointer template

    typedef mod<std::add_pointer, tuple_foo>::type tuple_ptrfoo;
    

    Or you can wrap it into a class so it is compatible with the first version

    // transforming a meta function into a meta function class
    template<template<typename...> class Metafun>
    struct ToClass {
      template<typename ... T>
      struct apply { typedef Metafun<T...> type; };
    };
    
    typedef mod<ToClass<std::add_pointer>, tuple_foo>::type tuple_ptrfoo;
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have multiple objects laying on each other, if I click on one
I have a 2-dimensional list of named tuples (let's say that each tuple has
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have some pixel buffer associated with variable: CVPixelBufferRef a; I want to
Say I have a string that's of the same form a tuple should be,
If I have a tuple such as (1,2,3,4) and I want to assign 1
Let's say I have a HasTraits object with a Tuple or a List attribute.
I have a tuple, INSTALLED_APPS, and I want to merge extra items into it
Ok, so let's say I have a type defined like so: type Foo =
Let's say I have a C++0x tuple: tuple<int,int,int> t(1,2,3); Now I can do the

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.