Getting type for a template

How might one go about solving this problem?
1
2
unknownType func( void );
std::vector<  /* type of unknownType */ > objects


The source of my question is trying to make a templated button:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
class Button
{
  T m_func;

  void onClick( void ) { m_func(); }
};

class Controller
{
  void updateSomething( void );
  
  Button< /* type of updateSomething */ > updateSomethingButton;
};


Thanks
Last edited on
closed account (o1vk4iN6)
Well you'd have to make updateSomething a static function otherwise you'd have to send a Controller object to button.

Are you trying to use this as a callback for when the button is pressed ? It might be better not to template this and simply provide a specific function type to be called:

1
2
3
4
5
Button<void (*)(int)> button; // what now... what gets sent to int.

// ...

void Button<T>::onClick() { m_func(); } // err 


or simply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Button
{
public:
    using Function = std::function<void()>;

    Button(Function callback) : m_callback(callback) { }

private:

    Function m_callback;

    void onClick() { m_callback(); } // since this will always have to be void() anyways

};

class Controller
{
public:
    Controller() : button(std::bind(&Controller::updateSomething, this)) { }

private:
    Button button;

    void updateSomething() { }

};


Thanks xerzi, I didn't realize that std::function<void()> would be able to handle the case of a this.
Topic archived. No new replies allowed.