SFML community forums

Help => General => Topic started by: kim366 on October 23, 2016, 06:03:03 pm

Title: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition
Post by: kim366 on October 23, 2016, 06:03:03 pm
Hi,

I am trying to understand std::functions, so I tried the following:

auto bp = sf::Mouse::isButtonPressed;

and that works fine.

I tried the something similar:

auto gp = sf::Mouse::getPosition;

and it gave me the error
unable to deduce 'auto' from 'sf::Mouse::getPosition'

when I switched
auto
to
std::function<sf::Vector2i(const sf::Window&)>

I got
error: conversion from '<unresolved overloaded function type>' to non-scalar type 'std::function<sf::Vector2<int>(const sf::Window&)>' requested

What am I doing wrong? I am sure, I am misunderstanding this whole concept, but I find it wierd, that the first one worked without a problem and the other one didn't. Is it because of the overload?
Title: Re: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition
Post by: korczurekk on October 23, 2016, 06:41:23 pm
There are 2 possible overloads of that function and compiler couldn't deduce which one to use. Hover I'm not really sure how to specify that, maybe some casting could make it work?
Title: AW: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition
Post by: eXpl0it3r on October 23, 2016, 07:18:00 pm
We like to keep this forum focused on questions related to SFML. There are many other sites out there that focus on C++ in general (e.g. StackOverflow).

Additionally you should consult a C++ resource (such as a book, C++ reference or online article) when trying to learn a new language feature. C++ just can't be learned by trial and error (and asking others to spend their time explaining things to you).

Do you know what the syntax of std::function is? What shoould be put between the <>? What can std::function hold? How do you call a function held by std::function?
Title: Re: std::function from sf::Mouse::isButtonPressed and sf::Mouse::getPosition
Post by: kim366 on October 24, 2016, 02:43:27 pm
Okay, sorry about that. I just didn't want to ask it there, because from my own experimentation everything else worked, but I just overlooked the const before the sf::Window.

Have a nice day!