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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:05:51+00:00 2026-05-29T16:05:51+00:00

I’m working on a C++ library and was wondering about compatibility within it’s own

  • 0

I’m working on a C++ library and was wondering about compatibility within it’s own API and ABI. I stumbled upon this (Creating Library with backward compatible ABI that uses Boost) which gave some helpful tips.

The problem I’m facing is trying to create a C interface that doesn’t use STL/Boost and somehow get the same functionality as the backend which does use them. Some of my interface functions require allowing the user to bind function pointers. Normally I’d use boost bind/function for this but now I can’t.

An example to illustrate the problem:

PRIVATE/INTERNAL:

class TestClass
{
public: 
    typedef boost::function<void (void)> FuncType;

public:
    TestClass( FuncType f ) : m_Func(f)
    {
    }

    void operator()()
    {
        m_Func();
    }

private:
    boost::function<void (void)> m_Func;
};

void* createTestClass(...)
{
    TestClass* fooHandle = new TestClass(boost::bind(...));
    return fooHandle;
}

PUBLIC INTERFACE:

extern "C" void* createTestClass(...);

CLIENT’S CODE:

void doSomething()
{
}

class DoStuffClass
{
public:
    void Stuff() {}
};

createTestClass(&doSomething);

DoStuffClass stuff;
createTestClass(&DoStuffClass::Stuff, &stuff);

I can probably make this work for simple function pointers but for member functions is there a way? What would I put in the ... sections?

Cheers!

  • 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-29T16:05:52+00:00Added an answer on May 29, 2026 at 4:05 pm

    If you’re going to try to define your own delegates to wrap C++ member functions, free functions etc., I suggest you take a look at “5 years later, is there something better than the “Fastest Possible C++ Delegates”?. The discussion is quite nice and the accepted answer proposes a neat trick to reduce all callbacks to a pair of void* and some free function R(void*,...) that invokes a member function or free function and whatnot.

    I’ve used a similar technique to implement wrappers for Win32 API callbacks. Here’s an example (take a look at the Timer::function<> and Timer::method<> inner types). Using this technique, you can expose a C interface like:

    /* type of callback functions passed through the C interface.
       Obviously, C++ templates won't be of any help here, you need
       to typedef all the function signatures. */
    typedef void(*Callback)(void*,int, int);
    
    /* Example function that uses one of the callbacks. */
    extern "C" void* createTestClass(Callback,void*);
    
    // Enhanced interface for passing object methods bound to 
    // a specific instance instead of the basic C-style callback.
    #ifdef __cplusplus
        template<typename T, void(T::M)(int,int)>
        inline
        void * createTestClass2(T& object,method<T,M> method)
        {
            // '&object' converts to 'void*' and 'method' object
            // converts to 'void(void*,int,int)' function pointer.
            return createTestClass(&object, method);
        }
    #endif
    

    The role of the proposed mechanism is just generation of the void(void*,int,int) functions that wrap member function calls so that they have the right signature. Everything else is just plain C function pointers and can be freely passed to your C API while preserving the ABI.

    Just be careful exceptions don’t leak from your C++ callback functions. See “Passing C++ exceptions across a C API boundary” for a more detailed discussion of the topic.


    Update (working example)

    Here’s a full working example (tested with Visual Studio 2008).

    library.h (public interface for C or C++ clients with stable ABI)

    #ifndef _library_h_
    #define _library_h_
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    typedef int(*Callback)(void*,int,int);
    
    void* createTestClass(Callback,void*);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif /* _library_h_ */
    

    library.c (library implementation)

    This demonstrates a C implementation, but you could have a C++ file instead and use std::function<>, boost::function<> or other to use the callback.

    #include "library.h"
    
    void* createTestClass(Callback callback, void* context)
    {
        callback(context, 1, 2);
        return 0;
    }
    

    library.hpp (enhanced interface for C++ clients, builds on public ABI)

    This is the enhanced interface for C++ clients. It adds support for using member functions instead of plain int(void*,int,int) free functions. If you only plan to support C++ clients, then this can be in the same file as your C-style interface.

    #ifndef _library_hpp_
    #define _library_hpp_
    
    #include "library.h"
    
    template<typename T, int(T::*M)(int,int)>
    class MethodCallback
    {
        static int wrapper(void* context, int x, int y)
        {
            return (static_cast<T*>(context)->*M)(x, y);
        }
    public:
        operator Callback () const
        {
            return &wrapper;
        }
    };
    
    template<typename T, int(T::*M)(int,int)>
    void* createTestClass(T& object, MethodCallback<T,M> method)
    {
        return createTestClass(static_cast<Callback>(method), static_cast<void*>(&object));
    }
    
    #endif //  _library_hpp_
    

    main.cpp (test program)

    Here’s a simple demo on using the interface:

    #include "library.hpp"
    #include <iostream>
    
    namespace {
    
        class Foo
        {
        public:
            int bar(int x, int y)
            {
                std::cout
                    << "Foo::bar(" << x << "," << y << ")"
                    << std::endl;
                return 1;
            }
        };
    
    }
    
    int main(int, char**)
    {
        Foo foo;
        void* instance = ::createTestClass
            (foo, MethodCallback<Foo, &Foo::bar>());
        // ...
    }
    

    You should expect this program to output Foo::bar(1, 2) since the registered callback function will invoke foo.bar(1,2).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.