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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:27:18+00:00 2026-05-30T16:27:18+00:00

Imagine I have the following free function and functor: void myFreeFunction(void) { cout <<

  • 0

Imagine I have the following free function and functor:

void myFreeFunction(void)
{
    cout << "Executing free function" << endl;
}

struct MyFunctor
{
    void operator()(void)
    {
        cout << "Executing functor" << endl;
    }
};

As discribed by this answer, I can pass my function or functor as a template argument to another function:

template <typename F>
void doOperation(F f)
{
    f();
}

And then call:

doOperation(myFreeFunction);
doOperation(MyFunctor());

So far so good. But what if I want something like the following:

template<typename Callback>
class MyClass
{
private:
    Callback mCallback;

public:
    MyClass(){}

    void execute()
    {
        mCallback();
    }
};

In this case I’m specifing the function/functor when I declare the class but not calling it until later. It works for functors:

MyClass<MyFunctor> myClass1;
myClass1.execute();

But not for functions:

MyClass<myFreeFunction> myClass2;
myClass2.execute();

Compiler says:

error C2923: ‘MyClass’ : ‘myFreeFunction’ is not a valid template type argument for parameter ‘Callback’

Which is fair enough… but how would you structure this?

Note: I’m aware of std::function and may end up using this. It’s measurably slower though so I’m looking at all options.

Thanks,

David

  • 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-30T16:27:19+00:00Added an answer on May 30, 2026 at 4:27 pm

    The problem is that freefunction is not a type but a element of a of (int this case a function pointer.

    to fix this problem you need to pass the function in, in construction, however you still need to know the exact type.

    myclass<call_back_t> my_class(call_back);
    

    EDIT: In c++11 the type can be got from decltype(call_back)

    however getting the call back can be troublesome it is often a lot easier to create a generator function

    //this should be in the namespace of the class or a static member of it
    template<FuncType>
    myclass<FuncType> make_class(FuncType func)
    {
         return myclass<FuncType>(func);
    }
    //called like
    myclass mc=make_class(&my_callback);
    

    Don’t for get to alter the constructor

    template<typename CallBack>
    myclass{
    private:
       CallBack call_back;
    public:
       myclass(CallBack call_back_)
       : call_back(call_back_)
       {}
    };
    

    or something like that

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

Sidebar

Related Questions

In Java 6 , imagine I have the following method signature: public void makeSandwich(Bread
Imagine I have the following situation: File1.php <?php include(Function.php); log(test); ?> Function.php <?php function
Imagine that I have the following two methods: void AddEvenNumbers() { for(int i=0; i<10000000;
Imagine I have the following: inFile = /adda/adas/sdas/hello.txt # that instruction give me hello.txt
Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new
Imagine I have the following code (simplified regarding my real context of course): <div
I came across the following weird chunk of code.Imagine you have the following typedef:
This is probably a silly question, but here it goes.Imagine you have the following
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
Imagine the following problem: You have a database containing about 20,000 texts in a

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.