Having the function definition:
void f(int) { }
I want to define:
int a;
but if the function definition c
How about making the template function?
template <typename T>
void f(T t);
You can get the type by template metaprogramming:
template <class F> struct ArgType;
template <class R, class T>
struct ArgType<R(*)(T)> {
typedef T type;
};
void f(int) {}
#include <type_traits>
#include <iostream>
int main() {
// To prove
std::cout << std::is_same< ArgType<decltype(&f)>::type, int >::value << '\n';
// To use
ArgType<decltype(&f)>::type a;
}
Depending on where you want to use it you'd need to specialize this litte template for other callable entities such as member function poitners, functions with more arguments, functors etc. There are more sophisitcated approaches in the Boost libraries, see e.g. https://stackoverflow.com/a/15645459/1838266
Caveat: all these utilities work only if the name of the function/callable is unambiguously mapped to one single function signature. If a function is overloaded or if a functor has more than one operator()
, the right function/operator has to be picked by explicitly casting to the right signature, which makes finding out part of the signature via the template pretty useless. This applies in a certain way to templates as well, although getting the signature of an explicitly secialized callable might still be useful, e.g.:
template <unsigned N, class F> struct ArgType; //somewhat more sophisitcated
template <class T> void f(int, T);
ArgType<0, decltype(&f<double>)> //int - ArgType has it's use here
ArgType<1, decltype(&f<double>)> //double - here it's useless...
It depends on what you want to do, where that variable shall be used. If it is in the function a template might be a good choice:
template<typename T>
void foo(T ) {
T a;
}
Alternatively if you are outside the function and have the requirement to really know this you can use Boost.TypeTraits, i.e. function_traits<void (int)>::arg1_type
will give int
Use a template?
template< typename T >
void f( T )
{
T a;
}
One of approaches is to use typedef for the type of the function parameter. For example
typedef int TParm;
void f( TParm );
TParm a;
You can select any name for the type. For example parm_t
and so on. It is important that there will not be a name collision.
In this case you will need to change only the typedef if you want to change the type of the parameter.
Or if your compiler supports aliases you can also write
using TParm = int;
void f( TParm );
TParm a;
Also you can wrap the function in a namespace or class.:) For example
struct IFunction
{
typedef int parm_t;
static void f( parm_t = parm_t() ) {}
};
//...
IFunction::parm_t a;
IFunction::f( a );