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

  • Home
  • SEARCH
  • 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 7677063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:20:21+00:00 2026-05-31T17:20:21+00:00

Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;

  • 0

Consider this code:

#include <iostream>
using namespace std;

class hello{
public:
    void f(){
        cout<<"f"<<endl;
    }
    virtual void ff(){
        cout<<"ff"<<endl;
    }
};

#define call_mem_fn(object, ptr)  ((object).*(ptr))

template<R (C::*ptr_to_mem)(Args...)> void proxycall(C& obj){
    cout<<"hello"<<endl;
    call_mem_fn(obj, ptr_to_mem)();
}

int main(){
    hello obj;
    proxycall<&hello::f>(obj);
}

Of course this won’t compile at line 16, because the compiler doesn’t know what R, C and Args, are. But there’s another problem: if one tries to define those template parameters right before ptr_to_mem, he runs into this bad situation:

template<typename R, typename C, typename... Args, R (C::*ptr_to_mem)(Args...)> 
                             //  ^variadic template, but not as last parameter!
void proxycall(C& obj){
    cout<<"hello"<<endl;
    call_mem_fn(obj, ptr_to_mem)();
}

int main(){
    hello obj;
    proxycall<void, hello, &hello::f>(obj);
}

Surprisingly, g++ does not complain about Args not being the last parameter in the template list, but anyway it cannot bind proxycall to the right template function, and just notes that it’s a possible candidate.

Any solution? My last resort is to pass the member function pointer as an argument, but if I could pass it as a template parameter it would fit better with the rest of my code.

EDIT:
as some have pointed out, the example seems pointless because proxycall isn’t going to pass any argument. This is not true in the actual code I’m working on: the arguments are fetched with some template tricks from a Lua stack. But that part of the code is irrelevant to the question, and rather lengthy, so I won’t paste it here.

  • 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-31T17:20:22+00:00Added an answer on May 31, 2026 at 5:20 pm

    You could try something like this:

    template <typename T, typename R, typename ...Args>
    R proxycall(T & obj, R (T::*mf)(Args...), Args &&... args)
    {
        return (obj.*mf)(std::forward<Args>(args)...);
    }
    

    Usage: proxycall(obj, &hello::f);

    Alternatively, to make the PTMF into a template argument, try specialization:

    template <typename T, T> struct proxy;
    
    template <typename T, typename R, typename ...Args, R (T::*mf)(Args...)>
    struct proxy<R (T::*)(Args...), mf>
    {
        static R call(T & obj, Args &&... args)
        {
            return (obj.*mf)(std::forward<Args>(args)...);
        }
    };
    

    Usage:

    hello obj;
    
    proxy<void(hello::*)(), &hello::f>::call(obj);
    
    // or
    
    typedef proxy<void(hello::*)(), &hello::f> hello_proxy;
    hello_proxy::call(obj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider this: #include <iostream> using namespace std; class A{ protected: void some_function(int params) {
Consider the sample code below: #include <iostream> using namespace std; class core { public:
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Consider the following sample code below: #include <iostream> using namespace std; class base {
please consider following code #include <iostream> using namespace std; class Digit { private: int

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.