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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:33:27+00:00 2026-06-17T05:33:27+00:00

I have a template class Delegate, with an overloaded += operator, that makes using

  • 0

I have a template class Delegate, with an overloaded += operator, that makes using delegates similar to C#.

// ... generalized version of the template omitted from code
template<typename... TArgs>
    class Delegate<void, TArgs...>
    {
        private:
            using Func = std::function<void(TArgs...)>;
            std::vector<Func> funcs;

        public:
            template<typename T> Delegate& operator+=(T mFunc) { funcs.push_back(Func(mFunc)); return *this; }
            void operator()(TArgs... mParams) { for (auto& f : funcs) f(mParams...); }
    };

This is what I’m trying to do:

struct s
{
    void test() { }
    void run()
    {
        Delegate<void> d;
        d += [] { /* do something */ ; };
        d += test; // does not compile
    }
};

Is there a way to allow d += test; to work?

  • 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-17T05:33:28+00:00Added an answer on June 17, 2026 at 5:33 am
    void test(int x, int y) { return x - y; }
    

    How can it compile? This function is supposed to return nothing. Its return type is void.

    Also, I assume you have defined (or declared ) the primary template:

    template<typename R, typename... TArgs>
    class Delegate;
    

    Also assuming that delegate is a typo, as the class template is Delegate.

    Anyway, with test returning nothing, it compiles fine:

    http://stacked-crooked.com/view?id=c56b7a2e758f8fbc361228834c90822b


    As for member-function-pointers, your current implementation doesn’t support it. Note that a non-static member function pointer takes the form of R (C::*MemPtr)(Args...) cv. Just work on it.

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

Sidebar

Related Questions

I have a template class A, and a stream operator function for A that
Say I have a template class that takes msgs from source, does something smart
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have the template class and array of pointers to objects and overloaded logic
I have a template class with a variadic template member function that I am
I want to have a template class that looks something like what I have
I have a template class that is: template <class identifier,class registeredObject> class FxPairRegistry :
I have a template class Foo that takes two (or more) template arguments. I
I have a template class that defines a subtype. I'm trying to define the
I have a template class that I've subclassed with a pointer to it (Decorator

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.