如何可以推导出C ++中的函数参数类型? [英] How is possible to deduce function argument type in C++?

查看:173
本文介绍了如何可以推导出C ++中的函数参数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有函数定义:

  void f(int){} 



我要定义:

  

但是如果函数定义更改为:

  void f(double){} 

必须成为:

  double a; 

即,a的类型必须与f 功能。
我需要类似下面的东西:

  decltype_of_argument< f,0&一个; 

在C ++中可以吗?

解决方案

您可以通过模板元编程获取类型:

  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(){

//证明
std :: cout< std :: is_same< ArgType< decltype(& f)> :: type,int> :: value< '\\\
';

//使用
ArgType< decltype(& f)> :: type a;
}

根据您要使用的位置,其他可调用实体的模板,例如成员函数poiters,具有更多参数的函数,函子等。Boost库中有更复杂的方法,参见例如 http://stackoverflow.com/a/15645459/1838266



警告:,所有这些实用程序只有在函数/可调用的名称明确映射到一个单一函数签名时才会工作。如果一个函数被重载或者如果一个函子有多个 operator(),则必须通过显式转换到正确的签名来选择正确的函数/操作符,通过模板签出的部分相当无用。这也适用于模板,虽然获得一个明确的secialized可调用的签名可能仍然有用,例如:

  template< unsigned N,class F> struct Argtype; //更加复杂

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 - 这里是没用的...


Having the function definition:

void f(int) { }

I want to define:

int a;

but if the function definition changes to:

void f(double) { }

the variable definition must become:

double a;

that is, the type of "a" must be the same of the first argument of the "f" function. I need something like the following:

decltype_of_argument<f, 0> a;

Is it possible in C++?

解决方案

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. http://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...

这篇关于如何可以推导出C ++中的函数参数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆