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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:49:16+00:00 2026-06-11T18:49:16+00:00

So I have the following code: #include <iostream> template <typename T> class funcky {

  • 0

So I have the following code:

#include <iostream>

template <typename T>
class funcky
{
  public:
    funcky(char const* funcName, T func)
      : name(funcName), myFunc(func)
    {
    }

  //private:
    char const* name;
    T myFunc;
};

#if 0
int main(void)
{
  char const* out = "nothing";

  // requires template args
  funcky test("hello", [&](int x, int y) -> int
  {
    out = "YES";
    return x + y;
  });

  std::cout << test.name << " = " << test.myFunc(1, 2) << std::endl;
  std::cout << test.name << " = " << out << std::endl;

  return 0;
}

int main2(void)
{
  funcky<void(*)(void)> test("hello", [&, this](void) -> void
  {
    std::cout << this->name << std::endl;
  });

  test.myFunc();

  return 0;
}
#endif

int main(void)
{
  char const* out = "nothing";

  auto myFunc = [&](int x, int y) -> int
  {
    out = "YES";
    return x + y;
  };
  funcky<decltype(myFunc)> test("hello", myFunc);

  std::cout << test.name << " = " << test.myFunc(1, 2) << std::endl;
  std::cout << test.name << " = " << out << std::endl;

  return 0;
}

The top chunk is a function holder that holds a lambda and a name for it.

Next is what I’d like to use API-wise, but fails due to no template arguments being specified.

After that, there’s my wondering if it’s possible to have a ‘this’ of a specific type (such as funcky) be used in a lambda not declared inside it. Wishful thinking.

At the very end is code that compiles but uses a lambda outside the funcky constructor and decltype.

Are such things possible in C++11? How I accomplish said things?

Also unless it can kind of have the same API, try not to guess what I’m doing as if I can’t do it this way, I’ll just rewrite it in a simpler way. It’s not worth the effort.

  • 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-11T18:49:17+00:00Added an answer on June 11, 2026 at 6:49 pm

    If you want to provide a way for a user to supply a callback to your class, you’re better off using std::function, since templating the class on the function / functor type is not a very useful thing to do, as you experienced.

    The problem arises from the fact that you can’t just take anything in. You should have clear requirements on what can be passed as a callback, since you should know how you want to call it later on. See this on why I make the constructor a template.

    #include <functional>
    #include <utility>
    
    struct X{
      template<class F>
      X(F&& f) : _callback(std::forward<F>(f)) {} // take anything and stuff it in the 'std::function'
    
    private:
      std::function<int(int,int)> _callback;
    };
    
    int main(){
      X x([](int a, int b){ return a + b; });
    }
    

    If, however, you don’t know how the callback is going to be called (say, the user passes the arguments later on), but you want to support that, template your type on the signature of the callback:

    #include <iostream>
    #include <functional>
    #include <utility>
    
    template<class Signature>
    struct X{
      template<class F>
      X(F&& f) : _callback(std::forward<F>(f)) {} // take anything and stuff it in the 'std::function'
    
    private:
      std::function<Signature> _callback;
    };
    
    int main(){
      X<int(int,int)> x1([](int a, int b){ return a + b; });
      X<void()> x2([]{ std::cout << "wuzzah\n";});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following code #include <iostream> #include <string> template <typename T> class
I have the following code: #include <iostream> class Grandma { public: virtual void foo()
i have some trobles with the following code: #include <iostream> #incldue <vector> template <typename
I have the following test code #include <iostream> template <typename T> struct PS {
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have the following code: #include <iostream> using namespace std; template <class T> class
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
I have the following code compiled by gcc: #include <iostream> using namespace std; class
Consider the following code: #include <iostream> template<class T> struct outer { struct inner {};
I have the following code :- #include <iostream> #include <vector> using namespace std; template

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.