SFML community forums

Help => General => Topic started by: Flaco on June 29, 2014, 07:30:07 pm

Title: [Solved] I don't get this code snippet from the book SFML Game Development
Post by: Flaco on June 29, 2014, 07:30:07 pm
Greetins y'all !  :D

Well; I'm actually reading SFML Game Development (http://www.packtpub.com/sfml-game-development/book) and in the chapter 4, sub-chapter "Handling Player Input" there's something I don't understand.

Here's the code (I explain what I don't understand below):

(click to show/hide)

Where the class Aircraft is (basically) a scene node handling its sf::Sprite, its type, etc...

So, what I don't get here is, if derivedAction() expects two parameters (SceneNode& and sf::Time), why in this snippet it just receives one argument (of type SceneNode&, I assume, since the code changes further in the chapter I can't really be sure it is one or just some "basic key word so it's simpler and lighter").

So yes, I know, as I just said, the code changes so it may just be an example but could somebody explain me if this code would work and (most of all) why?
I have read the documentation about std::function on cppreference (http://en.cppreference.com/w/cpp/utility/functional/function) and as I understood, this code is not legal.

Maybe I'm just taking a simple example too seriously, if so; I'm sorry to have bothered you. :D

Thank you !
Title: Re: I don't get this code snippet from the book SFML Game Development
Post by: Hapax on June 29, 2014, 07:36:36 pm
It looks to me as though derivedAction() expects one parameter - a function - and AircraftMover() is the function that expects the two parameters that you specified.
Title: Re: I don't get this code snippet from the book SFML Game Development
Post by: Flaco on June 29, 2014, 07:51:50 pm
Oh right, looks like I didn't fully understood the uses of std::function. :D Thank you!
But one mystery remains in my case... what is the purpose of the two parameters putted in the std::function declaration?

/* ... */

std::function<void(SceneNode& node, sf::Time dt)> foo;

/* ... */
 

I think I shall RTFM a second time. :)

EDIT: Read it on a bunch of sources, still didn't found a function wrapped by a std::function having "its own" parameters (I thought the <void(int, int)> were the wrapped element's parameters and return type).
Title: Re: I don't get this code snippet from the book SFML Game Development
Post by: Hapax on June 29, 2014, 08:22:08 pm
I think that derivedAction returns two values. Does the Aircraft class include SceneNode and sf::Time values?
Title: Re: I don't get this code snippet from the book SFML Game Development
Post by: Laurent on June 29, 2014, 08:28:14 pm
Why don't you read a tutorial about std::function? The book, nor this forum, won't teach you what it is and how it works ;)

std::function wraps a function, and the template parameter is the signature of the wrapped function.

void func(int, std::string);

std::function<void(int, std::string)> foo = func;

foo(5, "hello"); // calls func(5, "hello")
Title: Re: I don't get this code snippet from the book SFML Game Development
Post by: Flaco on June 29, 2014, 08:45:27 pm
Hello Laurent, thanks for your help.
I will read a tutorial about std::function, you are right. :)

I "kinda" see how it works; it was the parenthesis after the name of the std::function that confused me.
So, void(SceneNode& foo, sf::Time dt) is the lambda expression signature.
And derivedAction<Aircraft> is the derivedAction's parameter type (since Function is a generic type).

I get it, thank you Laurent, thank you Golden Eagle, now, it's time to read some tutorial. :D

(Good evening/day to you!)