Variadic template events telling me "too few arguments"

Started by
9 comments, last by Servant of the Lord 11 years, 1 month ago

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)($arguments...) and also (Handlers)(...$arguments) and (Handlers)($arguments), and it changes nothing, it still tells me too few arguments.Does anyone have experience with variadics?

Advertisement
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.

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)(3, 5.0f); then it works fine, so theproblem has to be in the syntax of (Handlers)(arguments...); , but I'm not quite sure what I'm doing wrong, all examples I found on the net do it like this (name ...)

Try using a different name for the two different usages of arguments vs arguments.
e.g.
template<typename... Arguments>
void SampleFunction(Arguments... parameters);

The outcome is the same when I rename them and remove the dollar symbol, maybe (Handlers)(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...

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.

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

It does support variadic templates and I did get it to work as a variadic template; the support may just not be entirely complete or correct.

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.

So using it for events with callbacks is not possible now?I guess I'll have to make it a normal template and a huge list of arguments

This topic is closed to new replies.

Advertisement