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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:42:32+00:00 2026-05-28T20:42:32+00:00

This is a follow up of this problem: Generic functor for functions with any

  • 0

This is a follow up of this problem: Generic functor for functions with any argument list

I have this functor class (full code see link above):

template<typename... ARGS>
class Foo
{
    std::function<void(ARGS...)> m_f;
public:
    Foo(std::function<void(ARGS...)> f) : m_f(f) {}
    void operator()(ARGS... args) const { m_f(args...); }
};

In operator() I can access the args... easily with a recursive "peeling" function as described in Stroustrup’s C++11 FAQ

My problem is: I want to access the types of the arguments of f, i.e. ARGS..., in the constructor. Obviously I can’t access values because there are none so far, but the argument type list is somehow burried in f, isn’t it?

  • 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-28T20:42:33+00:00Added an answer on May 28, 2026 at 8:42 pm

    You can write function_traits class as shown below, to discover the argument types, return type, and number of arguments:

    template<typename T> 
    struct function_traits;  
    
    template<typename R, typename ...Args> 
    struct function_traits<std::function<R(Args...)>>
    {
        static const size_t nargs = sizeof...(Args);
    
        typedef R result_type;
    
        template <size_t i>
        struct arg
        {
            typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
        };
    };
    

    Test code:

    struct R{};
    struct A{};
    struct B{};
    
    int main()
    {
       typedef std::function<R(A,B)> fun;
    
       std::cout << std::is_same<R, function_traits<fun>::result_type>::value << std::endl;
       std::cout << std::is_same<A, function_traits<fun>::arg<0>::type>::value << std::endl;
       std::cout << std::is_same<B, function_traits<fun>::arg<1>::type>::value << std::endl;
    } 
    

    Demo : http://ideone.com/YeN29

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

Sidebar

Related Questions

I have a two-step search problem. First I want to see if any matches
This is a follow up to my previous question: Problem passing parameters via Iframe
Our situation is as follows, but I'm curious about this problem in any situation.
I have a bunch of python methods that follow this pattern: def delete_session(guid): conn
I've got a static class (DataFormSubject) that holds a generic List object, as follows:
I have some problem with generics, let me explain. I have a class which
I have a generic linked list implementation with a node struct containing a void*
I'm using Entity Framework Code First. Usually I have no problem, but for a
I have an abstract generic view-model that I use as a base-class for several
You have a pseudo-table class and a pseudo-row class. The row is somewhat generic

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.