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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:40:00+00:00 2026-05-23T08:40:00+00:00

I currently write on an event handler system in C++0x. The handlers for each

  • 0

I currently write on an event handler system in C++0x. The “handlers” for each event (there can be multiple handlers) are registered by passing any kind of function/method that can be stored in a std::function object. This is done in C# style using the overloaded += operator. My Event-Class looks basically like this (stripped for better readability):

template<typename THandlerReturn, typename THandlerArgs...>
class Event {
    public:
        typedef THandlerReturn(HandlerSignature)(THandlerArgs...);
        typedef THandlerReturn(*HandlerFuntionPtr)(THandlerArgs...);
        typedef typename std::function<HandlerSignature> HandlerFunction;

        void operator += (HandlerFunction handler) {
            _handlers.push_back(handler);
        }

        // Some other methods for calling all handlers, ctors, etc.

    private:
        std::vector<HandlerFunction> _handlers;
};

Now I have another internal method for retrieving the actual function pointer from the std::function objects stored in the vector. It basically tries to do something like this:

// Preceeding: Get an iterator 'it' from the vector
HandlerFunctionPtr pt2func = it->target<HandlerSignature>();

At this point, when I use the Event class in my application, the compilation failes. Using a GCC 4.7 build I get the following error messages for the line above:

error: expected primary-expression before '>' token
error: expected primary-expression before ')' token

I’ve tested a little bit with the target<>() Method and the following works perfectly, assuming that the return type is void and there’s one argument of type int:

typedef void(Signature)(int);
typedef void(*FunctionPtr)(int);
std::function<Signature> func;
FunctionPtr pt2func = func.target<Signature>();

But as soon as there is one template argument involved, I get the above posted errors:

typedef THandlerReturn(Signature)(int);
typedef THandlerReturn(*FunctionPtr)(void);
std::function<Signature> func;
FunctionPtr pt2func = func.target<Signature>();

Can anyone tell me why that actually is, and if yes, if there’s any way to make it work? Any help and insights are very appreciated! Thanks in advance!

  • 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-23T08:40:01+00:00Added an answer on May 23, 2026 at 8:40 am

    You need to use template keyword as:

    HandlerFunctionPtr pt2func = it->template target<HandlerSignature>();
    

    Its because you’re calling a function template on an object whose type depends on a template argument. In the absence of the keyword template, the compiler is going to parse your code as,

    HandlerFunctionPtr pt2func = ( (it->target) < HandlerSignature ) >();
    

    which makes absolutely no sense. Hence the error.


    To know this in detail as to why template is needed, read this excellent explanation by @Johannes Schaub:

    • Where and why do I have to put the "template" and "typename" keywords?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read and write from serial using events/interrupts. Currently, I have it
I'd like to write a plugin that does something with the currently edited file
I'm currently working on a project that hooks into various system calls and writes
Currently I use this function, based on JCL code, which works fine: function IsDirectoryWriteable(const
I want to have generalised email templates. Currently I have multiple email templates with
I Delphi, I need a function which determinates if the system menu (resp. window
I wrote an application that currently runs against a local instance of MySql. I
My current knowledge: If you are trying to write text files in vbscript /
I'm trying to write a small script to change the current directory to my
I'm required to write documentation for my current project that lists all .c files

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.