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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:15:14+00:00 2026-05-21T09:15:14+00:00

The base class is : #include <memory> namespace cb{ template< typename R, typename …

  • 0

The base class is :

#include <memory>

namespace cb{

template< typename R, typename ... Args >
class CallbackBase
{
public:
    typedef std::shared_ptr< CallbackBase< R, Args... > >
            CallbackPtr;

    virtual ~CallbackBase()
    {
    }
    virtual R Call(  Args ... args) = 0;
};
} // namespace cb

Derived class is this :

namespace cb{
template< typename R, typename ... Args >
class FunctionCallback : public CallbackBase< R, Args... >
{
public:
    typedef R (*funccb)(Args...);

    FunctionCallback( funccb cb_ ) : 
        CallbackBase< R, Args... >(),
        cb( cb_ )
    {
    }
    virtual ~FunctionCallback()
    {
    }
    virtual R Call(Args... args)
    {
      return cb( args... );
    }
private:
  funccb cb;
};
} // namespace cb

Function to create :

namespace cb{
template < typename R, typename ...Args >
typename CallbackBase< R, Args... >::CallbackBasePtr
    MakeCallback( typename FunctionCallback< R, Args... >::funccb cb )
{
    typename CallbackBase< R, Args... >::CallbackBasePtr
        p( new FunctionCallback< R, Args... >( cb )
);
    return p;
}
} // namespace cb

And the example :

bool Foo_1args( const int & t)
{
    return true;
}
int main()
{
    auto cbObj = cb::MakeCallback( & Foo_1args );
}

I keep getting this error :

error: no matching function for call to ‘MakeCallback(bool (*)(const int&))’
error: unable to deduce ‘auto’ from ‘<expression error>’

I tried to change it, but I couldn’t figure out how to fix.

So, what is wrong? And how to fix this example?

  • 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-21T09:15:15+00:00Added an answer on May 21, 2026 at 9:15 am

    The problem might make sense with a simpler example. Try identifying the problem here:

    template <typename T>
    struct id { typedef T type; };
    
    template <typename T>
    void foo(typename id<T>::type x);
    
    foo(5); // error
    

    The problem is that the compiler cannot deduce what T should be; it’s not directly used anywhere. You’d have to explicitly provide it: foo<int>(5), or let it deduce it in some other way:

    template <typename T>
    void foo(typename id<T>::type x, T y);
    
    foo(5, 7); // okay, T is int because 7 is int
    

    This makes sense: how could the compiler figure out which T‘s, supplied to id, result in id<T>::type matching? There could be specializations, and the entire thing would be costly anyway, if possible.


    Likewise, there’s nothing the compiler has available to deduce R and Args. Instead, you should do this:

    template < typename R, typename ...Args >
    typename CallbackBase< R, Args... >::CallbackBasePtr
        MakeCallback( R cb(Args...) )
    {
        typename CallbackBase< R, Args... >::CallbackBasePtr
            p( new FunctionCallback< R, Args... >( cb ));
    
        return p;
    }
    

    Finally, you have other minor issues that need fixing, which Xeo has outlined.

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

Sidebar

Related Questions

#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <vector> #include <memory> using namespace std; class A { public: A(): i(new int)
Here is my code - #include<iostream> using namespace std; class base { public: void
#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}
#include <iostream> class base { public: virtual void print (int a) { std::cout <<
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
The following code prints 20, i.e. sizeof(z) is 20. #include <iostream.h> class Base {
My style of coding includes the following idiom: class Derived : public Base {
Imagine a base class with many constructors and a virtual method public class Foo

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.