mardi 18 février 2020

How can std::function's target be accessed? [duplicate]

I'm trying to compare the function pointer stored in two std::function objects, declared as std::function<void(Args...)> where Args... is a class template parameter. I intended to do this by comparing the output of target(), but I'm having trouble accessing that value. What's the correct way access std::function::target()?

Below is a boiled-down version of what I've tried:

#include <functional>

template <typename... Args>
void check(std::function<void(Args...)> func)
{
  auto type = func.target<void(*)(Args...)>();
}

void m() {}

int main()
{
  auto func = std::function<void()>(m);
  check<>(func);
}

And the output from compiling with g++ -std=c++11 Demo.cpp:

Demo.cpp: In function ‘void check(std::function<void(Args ...)>)’:
Demo.cpp:6:27: error: expected primary-expression before ‘void’
    6 |   auto type = func.target<void(Args...)>();
      |                           ^~~~
Demo.cpp:6:27: error: expected ‘,’ or ‘;’ before ‘void’

Aucun commentaire:

Enregistrer un commentaire