1

I have come across this code snippet in C++:

template <typename T, typename V>     
static void Check(V(T::*getter)(), identity_t<void(T::*)(V)>::type setter);

You can see it in the comment section of this post:

Template deduction from pointer-to-members, where at least one pointer-to-member is known

I have also seen similar snippets which look like this:

identity_t<void(some::input)>

I can't understand this syntax. Is identity_t taking in a function pointer of type void? What would the definition of identity_t look like?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dan
  • 2,694
  • 1
  • 6
  • 19
  • `identity_t` should be similar to [std::type_identity_t](https://en.cppreference.com/w/cpp/types/type_identity). That disallows deduction because of the `::type`. – Jarod42 Apr 27 '21 at 14:13
  • My question is the actual syntax. What does `void(some::input)` mean here, it it a function pointer of type void? – Dan Apr 27 '21 at 14:14
  • it is a signature: function taking `some::input`, and returning `void`, as you might find for `std::function`. – Jarod42 Apr 27 '21 at 14:15
  • is this syntax only used within templates? – Dan Apr 27 '21 at 14:18
  • Not necessary template: for pointer on function, you might use `void (*)(int);` you might also use intermediate typedef: `using callback_t = void(int); callback_t* c = my_func;`. – Jarod42 Apr 27 '21 at 14:21
  • similar question: https://stackoverflow.com/q/67159497/4117728 – 463035818_is_not_an_ai Apr 27 '21 at 14:24

0 Answers0