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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:05:06+00:00 2026-05-16T12:05:06+00:00

In Bjarne Stroustrup ‘s home page ( C++11 FAQ ): struct X { int

  • 0

In Bjarne Stroustrup‘s home page (C++11 FAQ):

struct X { int foo(int); };

std::function<int(X*, int)> f;
f = &X::foo; //pointer to member

X x;
int v = f(&x, 5); //call X::foo() for x with 5

How does it work? How does std::function call a foo member function?

The template parameter is int(X*, int), is &X::foo converted from the member function pointer to a non-member function pointer?!

(int(*)(X*, int))&X::foo //casting (int(X::*)(int) to (int(*)(X*, int))

To clarify: I know that we don’t need to cast any pointer to use std::function, but I don’t know how the internals of std::function handle this incompatibility between a member function pointer and a non-member function pointer. I don’t know how the standard allows us to implement something like std::function!

  • 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-16T12:05:07+00:00Added an answer on May 16, 2026 at 12:05 pm

    After getting help from other answers and comments, and reading GCC source code and C++11 standard, I found that it is possible to parse a function type (its return type and its argument types) by using partial template specialization and
    function overloading.

    The following is a simple (and incomplete) example to implement something like std::function:

    template<class T> class Function { };
    
    // Parse the function type
    template<class Res, class Obj, class... ArgTypes>
    class Function<Res (Obj*, ArgTypes...)> {
        union Pointers {
            Res (*func)(Obj*, ArgTypes...);
            Res (Obj::*mem_func)(ArgTypes...);
        };
    
        typedef Res Callback(Pointers&, Obj&, ArgTypes...);
    
        Pointers ptrs;
        Callback* callback;
    
        static Res call_func(Pointers& ptrs, Obj& obj, ArgTypes... args) {
            return (*ptrs.func)(&obj, args...);
        }
    
        static Res call_mem_func(Pointers& ptrs, Obj& obj, ArgTypes... args) {
            return (obj.*(ptrs.mem_func))(args...);
        }
    
      public:
    
        Function() : callback(0) { }
    
        // Parse the function type
        Function(Res (*func)(Obj*, ArgTypes...)) {
            ptrs.func = func;
            callback = &call_func;
        }
    
        // Parse the function type
        Function(Res (Obj::*mem_func)(ArgTypes...)) {
            ptrs.mem_func = mem_func;
            callback = &call_mem_func;
        }
    
        Function(const Function& function) {
            ptrs = function.ptrs;
            callback = function.callback;
        }
    
        Function& operator=(const Function& function) {
            ptrs = function.ptrs;
            callback = function.callback;
            return *this;
        }
    
        Res operator()(Obj& obj, ArgTypes... args) {
            if(callback == 0) throw 0; // throw an exception
            return (*callback)(ptrs, obj, args...);
        }
    };
    

    Usage:

    #include <iostream>
    
    struct Funny {
        void print(int i) {
            std::cout << "void (Funny::*)(int): " << i << std::endl;
        }
    };
    
    void print(Funny* funny, int i) {
        std::cout << "void (*)(Funny*, int): " << i << std::endl;
    }
    
    int main(int argc, char** argv) {
        Funny funny;
        Function<void(Funny*, int)> wmw;
    
        wmw = &Funny::print; // void (Funny::*)(int)
        wmw(funny, 10); // void (Funny::*)(int)
    
        wmw = &print; // void (*)(Funny*, int)
        wmw(funny, 8); // void (*)(Funny*, int)
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bjarne Stroustrup writes in his C++ Style and Technique FAQ , emphasis mine: Because

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.