I am trying to deduce callback function parameters from a callback given as a template parameter. I have found this previous answer: Can you extract types from template parameter function signature which looks like it'd be the answer, but somehow I cannot get this to work for what I want.
I have the following code:
#include <tuple>
template<typename S>
struct signature;
template<typename R, typename... Args>
struct signature<R(Args...)>
{
using return_type = R;
using argument_type = std::tuple<Args...>;
};
template <auto callback>
void check_callback()
{
using callback_type = decltype(callback);
using arg1 = std::tuple_element_t<0, signature<callback_type>::argument_type>;
}
int main()
{
}
When compiling this with clang 6.0 as c++17 I get the following error:
error: template argument for template type parameter must be a type; did you forget 'typename'?
If I change the line defining callback_type to something like
using callback_type = void(int);
then it suddenly works, so given a valid function signature the code seems to work. Why won't it work when using decltype on a function coming from a template parameter?
Why won't it work when using decltype on a function coming from a template parameter?
Because in this case callback_type
is a dependent name, which depends on the template parameter callback
. Then you have to use the keyword typename
.
Inside a declaration or a definition of a template,
typename
can be used to declare that a dependent name is a type.
So change the code to
using arg1 = std::tuple_element_t<0, typename signature<callback_type>::argument_type>;
// ^^^^^^^^
using callback_type = void(int);
doesn't have such issue; callback_type
is an independent name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With