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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:35:51+00:00 2026-06-16T12:35:51+00:00

This question was closed as exact duplicate since I chose a misleading question title.

  • 0

This question was closed as exact duplicate since I chose a misleading question title. It was not wrong but suggested an issue often discussed, e.g. in this question. Since the content is about a more specific topic never covered on Stackoverflow I would like the question to be reopened. This happened now, so here goes the question.

I have given a function expecting three integer values as parameters length(int x, int y, int z);. I cannot modify this function, e.g. to accept a struct or tuple of whatever as single parameter.

Is there a way in C++ to write another function which can be used as single argument to the function above, like length(arguments());?

Anyhow the return type of that function arguments(); seems to need to be int, int, int. But as far as far as I know I can’t define and use functions like this in C++. I know that I could return a list, a tuple, a struct or a class by arguments(). The question was closed because some people thought I would have asked about this. But the difficult part is to pass the tuple, or struct, or whatever as the three given integer parameters.

Is this possible and if yes, how is that possible in C++? A solution making use of C++11 would be fine.

  • 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-06-16T12:35:52+00:00Added an answer on June 16, 2026 at 12:35 pm

    I don’t think there is any direct way of doing what you want, but here is a C++11 technique that I use in several places of my code. The basic idea is to use a template function which I’ve called call_on_tuple to take a function argument f as well as a tuple of further arguments, expand the tuple and call the function on the expanded list of arguments:

    template <typename Fun, typename... Args, unsigned... Is>
    typename std::result_of<Fun(Args...)>::type
    call_on_tuple(Fun&& f, std::tuple<Args...>&& tup, indices<Is...>)
    { return f(std::get<Is>(tup)...); }
    

    So the idea is that instead of calling

    length(arguments());
    

    you would call

    call_on_tuple(length,arguments());
    

    This assumes that arguments() is changed so it returns a std::tuple<int,int,int> (this is basically the idea from the question you cited).

    Now the difficult part is how to get the Is... argument pack, which is a pack of integers 0,1,2,... used to number the elements of the tuple.

    If you are sure you’ll always have three arguments, you could use 0,1,2 literally, but if the ambition is to make this work for any n-ary function, we need another trick, which has been described by other posts, for example in several answers to this post.

    It’s a trick to transform the number of arguments, i.e. sizeof...(Args) into a list of integers 0,1,...,sizeof...(Args):

    I’ll put this trick and the implementation of call_on_tuple in a namespace detail:

    namespace detail {
    
      template <unsigned... Is>
      struct indices
      { };
    
      template <unsigned N, unsigned... Is>
      struct index_maker : index_maker<N-1,N-1,Is...>
      { };
    
      template <unsigned... Is>
      struct index_maker<0,Is...>
      { typedef indices<Is...> type; };
    
    
      template <typename Fun, typename... Args, unsigned... Is>
      typename std::enable_if<!std::is_void<typename std::result_of<Fun(Args...)>::type>::value,
                  typename std::result_of<Fun(Args...)>::type>::type
      call_on_tuple(Fun&& f, std::tuple<Args...>&& tup, indices<Is...>)
      { return f(std::get<Is>(tup)...); }
    }
    

    Now the actual function call_on_tuple is defined in global namespace like this:

    template <typename Fun, typename... Args>
    typename std::enable_if<!std::is_void<typename std::result_of<Fun(Args...)>::type>::value,
                typename std::result_of<Fun(Args...)>::type>::type
    call_on_tuple(Fun&& f, std::tuple<Args...>&& tup)
    {
      using std::tuple;
      using std::forward;
      using detail::index_maker;
    
      return detail::call_on_tuple
        (forward<Fun>(f),forward<tuple<Args...>>(tup),typename index_maker<sizeof...(Args)>::type());
    }
    

    It basically calls detail::index_maker to generate the list of increasing integers and then calls detail::call_on_tuple with that.

    As a result, you can do this:

    int length(int x, int y, int z)
    { return x + y + z; }
    
    std::tuple<int,int,int> arguments()
    { return std::tuple<int,int,int> { 1 , 2 , 3 }; }
    
    int main()
    {
      std::cout << call_on_tuple(length,arguments()) << std::endl;
      return 0;
    }
    

    which is hopefully close enough to what you needed.

    Note. I have also added an enable_if to ensure this is only used with functions f that actually return a value. You can readily make another implementation for functions that return void.

    Sorry again for closing your question prematurely.

    PS. You’ll need to add the following include statements to test this:

    #include <tuple>
    #include <type_traits>
    #include <iostream>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Closed as exact duplicate of this question . But reopened, as the other Singleton
Duplicate: This is the exact same question, from the exact same person, as Issue
edit: this question was closed as a duplicate, but the identified duplicate was asked
Closed as exact duplicate of this question . I have an array/list of elements.
I am risking this question being closed before i get an answer, but i
This question might be closed as it sounds vague but I'm really asking this
I asked this question earlier and it was closed because it was a duplicate,
I read a question earlier that was closed due to being an exact duplicate
I know this question will be closed because I studied many example of this
Duplicate of this question. Vote to close. Consider this at the windows commandline. scriptA.py

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.