Sign in to follow this  
mrheisenberg

Variadic template events telling me "too few arguments"

Recommended Posts

mrheisenberg    362

I'm using the new version of the compiler and decided to try out the variadic templates functionality to make an event class:


        template<typename... $arguments>
        class Event
        {
        public:
            std::vector<void (*)($arguments...)> Handlers;


        public:
            Event();
            ~Event();


            void Attach(void (*callback)($arguments...));
            void Detach(void (*callback)($arguments...));
            void DetachAll();


            void operator()();


            flow IsHandled() const;
        };
 

This is what it looks like, but when I use it in main.cpp:

 

    void TestFunc(int arg, float arg2)
{
    system("pause");
}
Event<int, float> evt;
evt.Attach(TestFunc);
evt();
 

It tells me "Error    1    error C2198: 'void (__cdecl *)(int,float)' : too few arguments for call"


This is what should happen when it's called:


        template<typename... $arguments>
        void Event<$arguments...>::operator()()
        {
            for(inti = 0; i < Handlers.size(); i++)
            {
                (Handlers)[i]();
            }
        }
 

I also tried with (Handlers)[i]($arguments...) and also (Handlers)[i](...$arguments) and (Handlers)[i]($arguments), and it changes nothing, it still tells me too few arguments.Does anyone have experience with variadics?

 

Share this post


Link to post
Share on other sites
SiCrane    11839
I don't understand what you expect this to do. You're trying to call a void (*)(int, float) with no arguments. Would you expect calling TestFunc with no arguments to work? Of course not, you need to give it an int and a float. Same with a function pointer that has int and float arguments. You probably want to modify operator() to accept arguments that you then pass to your handlers.

Share this post


Link to post
Share on other sites
mrheisenberg    362

I suppose I'm not getting the right syntax here, when I do:



 

        template<typename... $arguments>
        void Event<$arguments...>::operator()($arguments... arguments)
        {
            for(int i = 0; i < Handlers.size(); i++)
            {
                (Handlers)[i](arguments...);
            }
        }
 
It tells me "arguments : undeclared identifier" and " '...' there are no parameter packs available to expand" I tried putting the dots on both sides.I actually had the "there are no parameter packs available to expand" once before when I first tried them out, but back then people said it's a Visual Studio problem, yet now I see people saying they actually got it to work.



EDIT: If I do: (Handlers)[i](3, 5.0f); then it works fine, so theproblem has to be in the syntax of (Handlers)[i](arguments...); , but I'm not quite sure what I'm doing wrong, all examples I found on the net do it like this (name ...)
Edited by mrheisenberg

Share this post


Link to post
Share on other sites
mrheisenberg    362

The outcome is the same when I rename them and remove the dollar symbol, maybe (Handlers)[i](parameters...); is just incorrect, but I tried writing it any way possible, still gives the same error.The only similar error I found in google was some old unanswered post.Maybe I should test the same code with GCC...

Share this post


Link to post
Share on other sites
Brother Bob    10344

It compiles fine for me if you either implement the function in the class definition instead of separating the class and the function definition, or if you specialize the implementation of operator() for Event<int, float>. Otherwise, I get the errors you mention.

 

Thus, both

template<typename... Arguments> class Event {
    ...
    void operator()(Arguments... arguments)
    { ... }
};

and

template<typename... Arguments> class Event {
    ...
    void operator()(Arguments...);
};
 
void Event<int, float>::operator()(int, float)
{ ... }

works. I know the second one is not desired since you have to specify all the types you'll ever going to use. Could be that VS just doesn't handle variadic templates correctly since two other options work.

Share this post


Link to post
Share on other sites
mrheisenberg    362

Well they do state it supports variadic templates http://blogs.msdn.com/b/vcblog/archive/2012/11/02/visual-c-c-11-and-the-future-of-c.aspx


The November 2012 CTP release is available immediately for download here: http://aka.ms/vc-ctp. It contains the following C++11 additions:

  • Variadic templates

 

Maybe there's a specific bug that just manifests itself when trying to use it in the way I'm trying(setting function callbacks).However I thought that was supposed to be their main intended usage biggrin.png

Share this post


Link to post
Share on other sites

This code works, so you can use it as a test-case. It was given by one of the standard C++ committee members during a presentation, and is supposed to work in both GCC and Visual Studio:

 

//std::make_unique implementation (it was forgotten in the C++11 standard, and will be added later).
//Once it's added, I can just remove this from here.
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}

 

Usage:

 

std::unique_ptr<MyClass> myClassPtr = make_unique<MyClass>("meow", 357.0f);

 

This definitely works in the latest MinGW/GCC (4.7.2) - I use it in my code, but I think I've been using it since GCC 4.6.3.

I use this same syntax for three different unrelated functions in my code. Does this exact example compile and work for you?

 

SiCrane's absolutely right though: Even with variadic templates, that doesn't make your "void TestFunc(int arg, float arg2)" variadic - so unless you pass an int and a float, it'd fail to compile.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this