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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:33:33+00:00 2026-05-28T07:33:33+00:00

Is it possible to have multiple specializations of a variadic template where one of

  • 0

Is it possible to have multiple specializations of a variadic template where one of the template parameters is a statically bound member function pointer?

I’m attempting to build a delegate where the callback function is a compile time constant – thereby aiding the optimizer to see past the function pointer boundary.

I have the following code where I pass a member function pointer as a template parameter, and since the function pointer is a constant which is known at compile-time, my expectation is that the optimizer will be able to work through the function pointer boundary.

I have created 2 delegates, delegate0 and delegate1, which are for member functions which have 0 and 1 arguments respectively.

#include <iostream>

template<class class_t, void (class_t::*mem_func_t)()>
struct delegate0
{
  delegate0( class_t *obj_ )
      : _obj(obj_)
  { }

  void operator()()
  {
    (_obj->*mem_func_t)();
  }

 private:
  class_t *_obj;
};

template<class class_t, typename arg0, void (class_t::*mem_func_t)(arg0)>
struct delegate1
{
  delegate1( class_t *obj_, arg0 a0_ )
      : _obj(obj_)
      , _a0(a0_)
  { }

  void operator()()
  {
    (_obj->*mem_func_t)(_a0);
  }

 private:
  class_t *_obj;
  arg0 _a0;
};

struct app
{
  void cb()
  {
    std::cout << "hello world\n";
  }
  void cb1(int i)
  {
    std::cout << "hello world " << i << "\n";
  }
};

int main()
{
  app* foo = new app;

  delegate0<app, &app::cb> f(foo);
  f();

  delegate1<app, int, &app::cb1> f1(foo, 5);
  f1();
}

However, I would like to improve on this in 2 ways:

  1. All permutations of the number of arguments to be specializations of a variadic delegate template.
  2. Use template argument deduction such that declaring something like delegate<&app::cb> (when cb is not ambiguous), class_t, mem_func_t, arg0, arg1, etc… are all deduced from the signature for app::cb.

I realize that a member function pointer is not a type, but just like you can pass a particular integer as a template parameter (ala template recursion used in metaprogramming), I figure you can have a specific member function pointer as a parameter – thereby allowing static binding to that function.

Is what I’m after even possible?
If not, is either of 1 or 2 above possible?
I would really appreciate a working example, because I’ve been banging my head against my keyboard with no success as of yet.

I have the following miserable attempt. It is clearly not what I’m looking for, but in order to show the direction I’ve been heading, I thought it perhaps useful to include.

template<typename...>
struct delegate;

template<class class_t, void (class_t::*mem_func_t)()>
struct delegate<class_t, decltype(mem_func_t)> 
{
  delegate( class_t *obj_ )
      : _obj(obj_)
  { }

  void operator()(mem_func_t f)
  {
    (_obj->*f)();
  }

  class_t *_obj;
};

template<class class_t, typename arg0, void (class_t::*mem_func_t)(arg0)>
struct delegate<class_t, arg0, decltype(mem_func_t)>
{
  delegate( class_t *obj_, arg0 a0_ )
      : _obj(obj_)
      , _a0(a0_)
  { }

  void operator()()
  {
    (_obj->*mem_func_t)(_a0);
  }

  class_t *_obj;
  arg0 _a0;
};
  • 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-28T07:33:33+00:00Added an answer on May 28, 2026 at 7:33 am

    Declare a template taking any types:

    template <typename T, T value>
    struct Delegate;
    

    and then specialize it for member function objects (do it 4 times for each cv-qualifier):

    template <typename R, typename C, typename... A, R (C::* value)(A...) const>
    struct Delegate<R(C::*)(A...) const, value>
    {
       // do whatever you like with R, C, A... .
    };
    

    As I’ve answered before, you’ll need decltype:

    Delegate<decltype(&SomeClass::method), &SomeClass::method> del;
    

    Alternatively, you could use my function_traits class which can extract the R, C and A… from T directly so you don’t need to specialize, but decltype and repeating the method is still needed.

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

Sidebar

Related Questions

is it possible to have multiple implementors with only one address ? So something
Is it possible to have multiple Alfresco instances sharing one common folder in the
Is it possible to have multiple ellipsis arguments in an R function? A simplified
In Python, is it possible to have multiple except statements for one try statement?
Is it possible to have multiple windows-handles open in one Adobe AIR application? You
http://jsfiddle.net/rHVcX/1/ Is it possible to have multiple click events on one elements, when using
Is it possible to have multiple contact forms on one page and still validate
Is it possible to have multiple view of the same display object? (e.g. same-computer
Is it possible to have multiple listeners to messages carried by MSMQ? WCF appears
Is it possible to have multiple folders where I can place applications to be

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.