• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mrheisenberg

Variadic template events telling me "too few arguments"

10 posts in this topic

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?

 

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

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
0

Share this post


Link to post
Share on other sites

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

0

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  
Followers 0