Jump to content

  • Log In with Google      Sign In   
  • Create Account

Variadic template events telling me "too few arguments"


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 22 February 2013 - 06:59 AM

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?

 



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9676

Like
0Likes
Like

Posted 22 February 2013 - 07:15 AM

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.

#3 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 22 February 2013 - 07:24 AM

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, 22 February 2013 - 08:05 AM.


#4 Hodgman   Moderators   -  Reputation: 32028

Like
0Likes
Like

Posted 22 February 2013 - 08:19 AM

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


Edited by Hodgman, 22 February 2013 - 08:37 AM.


#5 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 22 February 2013 - 08:50 AM

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



#6 Brother Bob   Moderators   -  Reputation: 8632

Like
0Likes
Like

Posted 22 February 2013 - 08:51 AM

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.



#7 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 22 February 2013 - 09:00 AM

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



#8 Brother Bob   Moderators   -  Reputation: 8632

Like
0Likes
Like

Posted 22 February 2013 - 09:06 AM

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.



#9 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
0Likes
Like

Posted 22 February 2013 - 10:09 AM

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, 22 February 2013 - 10:13 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 22 February 2013 - 02:54 PM

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



#11 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
0Likes
Like

Posted 22 February 2013 - 03:42 PM

I'm sure it is usable for events - nobody said otherwise. Does the fully functionable test case I posted work for your compiler?


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS