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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:40:32+00:00 2026-05-30T03:40:32+00:00

I was trying to built atop the thread here: Variable length template arguments list?

  • 0

I was trying to built atop the thread here: Variable length template arguments list?
to have a default Functor class, this is only of academic interest. My goal is to build a generic Fucntor class: given a class name, method name and argument types(of variable length), it builds a class that has an operator() method which takes variable number of arguments of type specified in template args and takes a pointer and applies the given method. Imagine a class thus:

class MyClass
{
 public:
   float Fraction( float n, int m)
   {
       return n/m;
   }
   int Increment(int n)
   {
       return n+1;
   }
} ;

And a templatized functor class that can be used in any function thus:

int k = FunctorClass<MyClass, Increment, int, int /*return type*/> (3);
assert(k == 4);
float l = FunctorClass<MyClass, Fraction,  float, int, float, /*return type*/> (4,3);
assert(l == (4/3));

Can such a functor class be constructed?
Sidenote: Cant use Variadic templates, (building in VS2010, no … template arguments)
Thanks for the help

  • 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-30T03:40:33+00:00Added an answer on May 30, 2026 at 3:40 am

    This is certainly doable, e.g. Boost bind() uses this approach under the hood. Without variadics you won’t get full generality, however, because you will be limited to a fixed number of template arguments and you need to type the implementation for each different number of arguments you want to support. Also, without rvalue references you won’t get perfect forwarding.

    That said, the way you are trying to use it won’t work: when stating the member functions, you can’t just name them. You need to obtain the correct member function point using e.g. &MyClass::Increment and &MyClass::Fraction. If the member function is overloaded, you need to disambiguate it.

    Since you apparently want to enable the use of this function object for non-static member functions, you also need to provide an object on which the member function is to be called. The most reasonable approach for this is to pass a reference to the object as a constructor argument of the function object class and to store it to be used whenever the function is being called. That is, the use looks somewhat different but it can be simplified with some sort of factory function. Here is a version which adjusts the various things and implements a corresponding function object template:

    #include <cassert>
    
    // -----------------------------------------------------------------------------
    
    template <typename T, T> class FunctorClass;
    
    template <typename RC, typename Class,
              RC (Class::*Member)()>
    class FunctorClass<RC (Class::*)(), Member>
    {
    public:
        FunctorClass(Class& object): object_(&object) {}
        RC operator()() const { return (this->object_->*Member)(); }
    private:
        Class* object_;
    };
    
    template <typename RC, typename Class, typename A0,
              RC (Class::*Member)(A0)>
    class FunctorClass<RC (Class::*)(A0), Member>
    {
    public:
        FunctorClass(Class& object): object_(&object) {}
        RC operator()(A0 a0) const { return (this->object_->*Member)(a0); }
    private:
        Class* object_;
    };
    
    template <typename RC, typename Class, typename A0, typename A1,
              RC (Class::*Member)(A0, A1)>
    class FunctorClass<RC (Class::*)(A0, A1), Member>
    {
    public:
        FunctorClass(Class& object): object_(&object) {}
        RC operator()(A0 a0, A1 a1) const { return (this->object_->*Member)(a0, a1); }
    private:
        Class* object_;
    };
    
    // -----------------------------------------------------------------------------
    
    class MyClass
    {
     public:
        int foo() { return 17; }
        float Fraction( float n, int m)
        {
            return n/m;
        }
        int Increment(int n)
        {
            return n+1;
        }
    };
    
    int main()
    {
        MyClass object;
        int i = FunctorClass<int (MyClass::*)(), &MyClass::foo>(object)();
        assert(i == 17);
    
        int k = FunctorClass<int (MyClass::*)(int), &MyClass::Increment>(object)(3);
        assert(k == 4);
        float l = FunctorClass<float (MyClass::*)(float, int), &MyClass::Fraction>(object)(4,3);
        assert(l == (4.0f/3));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to subclass the built-in file class in Python to add some extra
I'm trying to have the built in jQuery widgets apply to dynamic content, for
Hi I'm trying to built simple color identifying program. I have taken a image
I am trying to built something like this: http://tourdeflex.adobe.com/flex4.0/TextLayoutFrameworkBasic/sample2.html User can not copy content
I have built a java server app and am trying to get my android
I am trying to built a websocket application in IE9 but I have the
I have built a simple game in WP7 and I am trying to add
Trying to install this contact form: http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme I'm getting HUGE gaps between fields: http://themeforward.com/demo2/features/contact-form/
Im trying to built this app which simple consist of a ListView and different
I have built a custom Camera App and I am trying to change 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.