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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:04:43+00:00 2026-06-12T00:04:43+00:00

I am trying to write my own delegate system as a replacement for boost::functions

  • 0

I am trying to write my own delegate system as a replacement for boost::functions since the latter does a lot of heap-allocations which I profiled to be problematic.
I have written this as a replacement (simplified, the actual thing uses pooled memory and placement new but this is simple enough to reproduce the error):

template<class A, class B>
struct DelegateFunctor : public MyFunctor {
  DelegateFunctor(void (*fptr)(A, B), A arg1, B arg2) : fp(fptr), a1(arg1), a2(arg2) {}

  virtual void operator()() { fp(a1, a2); }

  void (*fp)(A, B);  // Stores the function pointer.
  const A a1; const B a2;  // Stores the arguments.
};

and this helper function:

template<class A, class B>
MyFunctor* makeFunctor(void (*f)(A,B), A arg1, B arg2) {
  return new DelegateFunctor<A,B>(f, arg1, arg2);
}

The weird thing happens here:

void bar1(int a, int b) {
  // do something
}

void bar2(int& a, const int& b) {
  // do domething
}

int main() {
  int a = 0;
  int b = 1;

  // A: Desired syntax and compiles.
  MyFunctor* df1 = makeFunctor(&bar1, 1, 2);

  // B: Desired syntax but does not compile:
  MyFunctor* df2 = makeFunctor(&bar2, a, b);

  // C: Not even this:
  MyFunctor* df3 = makeFunctor(&bar2, (int&)a, (const int&)b);

  // D: Compiles but I have to specify the whole damn thing:
  MyFunctor* df4 = makeFunctor<int&, const int&>(&bar2, a, b);
}

The compiler error I get for version C (B is similar) is:

error: no matching function for call to ‘makeFunctor(void (*)(int&, const int&), int&, const int&)’

which is weird because the compiler has, in its error message, in fact correctly deduced the types.

Is there any way I can get version B to compile? How does boost::bind get around this limitation?
I’m using GCC 4.2.1. No C++11 solutions please.

  • 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-06-12T00:04:44+00:00Added an answer on June 12, 2026 at 12:04 am

    Argument deduction strips off references. By matching the function pointer signature for A, we want to get int &, but by matching the actual argument, we want int, and so the deduction fails.

    One solution is to make the second type non-deduced, like so:

    #include <type_traits>
    
    template <typename R, typename A, typename B>
    R do_it(R (*func)(A, B),
            typename std::common_type<A>::type a,   // not deduced
            typename std::common_type<B>::type b)   // not deduced
    {
        return func(a, b);
    }
    

    Now A and B are determined purely by the function pointer’s signature:

    int foo(int &, int const &);
    
    int main()
    {
        int a = 0, b = 0;
        return do_it(foo, a, b);  // deduces A = int &, B = int const &
    }
    

    (Note that std::common_type<T>::type is the recommended idiom for the “identity type” whose sole purpose is to remove a template argument from argument deduction. This has previously been called things like identity<T>::type or alias<T>, but the standard library trait std::common_type serves this purpose just fine.)

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

Sidebar

Related Questions

I'm trying to write a program which retrieves my own posts and comments from
I'm trying to write my own nmea parser,since i only need some info from
I am trying to write my own MySQL storage engine which uses a key-value
I am trying to write my own asynchronous network client using boost::asio. One of
I am trying to write my own Protobuf frame decoder and I'm basing it
Well im trying to write my own custom control extender but i can't get
I'm trying to write my own toy My Toy Language -> MSIL compiler in
I am trying to write our own RIA services provider to expose data from
I am trying to write my own form validation script with jQuery. However, I
I'm trying to write my own protocol so that multiple servers can pass data

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.