Is it possible to deduce the type of a function parameter? For example, if I have:
void foo(int a);
I would like to deduce the type int
as the type of foo
's first parameter. A possible use could be:
foo( static_cast< decltype(/* ??? foo's first param ??? */) >(value) );
In this related question, the answers exploit having a member with the same type for deduction, so it does not directly deduce the function parameter type.
If the expression parameter is a call to a function or an overloaded operator function, decltype(expression) is the return type of the function. Parentheses around an overloaded operator are ignored. If the expression parameter is an rvalue, decltype(expression) is the type of expression.
In the C++ programming language, decltype is a keyword used to query the type of an expression. Introduced in C++11, its primary intended use is in generic programming, where it is often difficult, or even impossible, to express types that depend on template parameters.
Type inference or deduction refers to the automatic detection of the data type of an expression in a programming language. It is a feature present in some strongly statically typed languages. In C++, the auto keyword(added in C++ 11) is used for automatic type deduction.
Is it possible to deduce the type of a function parameter?
Sure.
With a type traits, by example (argType
)
template <typename>
struct argType;
template <typename R, typename A>
struct argType<R(A)>
{ using type = A; };
void foo(int a)
{ }
int main()
{
long value = 1L;
foo( static_cast<typename argType<decltype(foo)>::type>(value) );
}
If you're interrested in a little more generic solution, the following example show how create and use a type traits to detect the return type or the n-th argument type
#include <string>
template <std::size_t N, typename T0, typename ... Ts>
struct typeN
{ using type = typename typeN<N-1U, Ts...>::type; };
template <typename T0, typename ... Ts>
struct typeN<0U, T0, Ts...>
{ using type = T0; };
template <std::size_t, typename>
struct argN;
template <std::size_t N, typename R, typename ... As>
struct argN<N, R(As...)>
{ using type = typename typeN<N, As...>::type; };
template <typename>
struct returnType;
template <typename R, typename ... As>
struct returnType<R(As...)>
{ using type = R; };
long bar (int a, std::string const &)
{ return a; }
int main()
{
long valI = 1L;
char const * valS = "abc";
bar( static_cast<typename argN<0U, decltype(bar)>::type>(valI),
static_cast<typename argN<1U, decltype(bar)>::type>(valS) );
static_assert(
std::is_same<long,
typename returnType<decltype(bar)>::type>::value, "!");
}
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