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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:26:24+00:00 2026-05-24T20:26:24+00:00

What is the best way to write the readvals function in the following code

  • 0

What is the best way to write the readvals function in the following code without using Boost? Basically, it should get a tuple, call a specific function of it’s elemets and return the generated results as a tuple again.

Is there any C++0X-based Functor definition library for tuples?

template <class T>
struct A
{
    A(T _val):val(_val){}
    T ret() {return val;}
    T val;
};

template <typename... ARGS>
std::tuple<ARGS...> readvals(std::tuple<A<ARGS>...> targ)
{
    //???
}

int main(int argc, char **argv)
{
    A<int> ai = A<int>(5);
    A<char> ac = A<char>('c');
    A<double> ad = A<double>(0.5);


    std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);

    // assuming proper overloading of "<<" for tuples exists
    std::cout << readvals<int,char,double>(at) << std::endl;
    // I expect something like (5, c, 0.5) in the output
    return 0;
}

I have found questions on SO which deal partly with this problem (tuple unpacking, iterating over tuple elements, etc.), but it seems to me that there should be an easier solution compared to putting together all such solutions.

  • 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-24T20:26:25+00:00Added an answer on May 24, 2026 at 8:26 pm

    Is this what you are looking for? I hope you can at least use it as a starting point. There are just more ways to approach working with tuples and variadic templates than there are ways that lead to rome…

    #include <iostream>
    #include <tuple>
    
    template<class T>
    struct A
    {
        T t;
        A( const T& t_ ) : t(t_) { }
    };
    
    template<class T>
    T func( const A<T>&  t)
    {
        std::cout << __PRETTY_FUNCTION__ << " " << t.t << "\n";
        return t.t;
    }
    
    template<size_t N,class R,  class T>
    struct genh
    {
        static void gen( R& ret, const T& t )
        {
            std::cout << __PRETTY_FUNCTION__ << "\n";
            genh<N-1,R,T>::gen(ret,t);
            std::get<N>(ret) = func(std::get<N>(t));
        }
    };
    
    template<class R, class T>
    struct genh<0,R,T>
    {
        static void gen( R& ret, const T& t )
        {
            std::cout << __PRETTY_FUNCTION__ << "\n";
            std::get<0>(ret) = func(std::get<0>(t));
        }
    };
    
    template<class... T>
    std::tuple<T...> readvals( const std::tuple<A<T>...>& targ )
    {
        std::tuple<T...> ret;
        genh<sizeof...(T)-1,std::tuple<T...>,std::tuple<A<T>...>>::gen(ret,targ);
        return ret;
    }
    
    int main(int argc, const char *argv[])
    {
        A<int> ai = A<int>(5);
        A<char> ac = A<char>('c');
        A<double> ad = A<double>(0.5);
    
    
        std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);
    
        readvals(at);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What should be the best way to write code: 1) Dim current = Request.Path
What is the best way to write a function (or something DSLish) that will
What's the best way to write an application in order to get it working
What would be the best way to write a generic copy constructor function for
Following on from this question what would be the best way to write a
What is the best way to write the code ? (1) Like directly writing
What is the best way to write this function ontop of jQuery. Background: I'm
Can any one suggest me the best way to write code for uploading a
What's the best way to write following if condition in Ruby? if ( $response_code
I am trying to figure out the best way to write a PHP function

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.