Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of `struct decay<T, R(A..., ...)>`

template <typename T, typename R, typename ...A>
struct decay<T, R(A..., ...)> { using type = R(*)(A..., ...); };

What's the exact meaning of that? I need some help~

like image 370
John Smith Avatar asked Mar 27 '17 13:03

John Smith


2 Answers

int foo(int);
int bar(int, ...);

these are two different functions. foo is of type int(int). bar is of type int(int,...).

... is C-style varargs, not to be confused with variardic template arguments which also uses ....

template <typename T, typename R, typename ...A>
struct decay<T, R(A..., ...)> { using type = R(*)(A..., ...); };

This part of an implementation of an optimized version of std::decay within boost::hana. The typename T and T parts are red herrings, part of that optimization.

It is a specialization that matches R(A..., ...), where A... and R are deduced from a function signature.

If you passed double(int, char, ...) as the 2nd argument to this hana::details::decay, R would be double and A... would be int, char. And the ... would "match the C-style varags".

This particular specialization's purpose to to map function signatures that end in C-style varargs to pointers to the same signature. So it maps double(int, char, ...) to double(*)(int, char, ...).

C style varargs are not the same as template variardic arguments. They predate it.

like image 180
Yakk - Adam Nevraumont Avatar answered Nov 15 '22 07:11

Yakk - Adam Nevraumont


This specialization is one of the specializations that enact the decay of a function type to the corresponding pointer-to-function type, which mirrors the way function lvalues decay to function pointer prvalues.

This particular specialization is used for variable-argument functions (those whose parameter list ends in an ellipsis so that it accepts arguments that don't match any parameters).

like image 29
Kerrek SB Avatar answered Nov 15 '22 07:11

Kerrek SB