确定参数的数字和类型,以及返回函数类型参数的类型
Determining number and type of parameters and return type of type parameter that is a function
给定一个C 11函数:
X f(A, B, C);
在此功能中,无论如何都存在:
template<typename T>
void g(T t)
{
...
}
称为如下:
g(f);
确定:
- f 的参数数量
- f 的参数i的类型
- f 的返回类型
...
template<typename F>
void g(F f)
{
constexpr size_t n = num_params<F>::n; // 3
return_type<F>::type x; // X
tuple<param_types<F>::type...> a; // tuple<A, B, C>
}
?
确定:
template <typename R, typename ...Args>
void g(R(&f)(Args...))
{
typedef R return_type;
unsigned int const n_args = sizeof...(Args);
// ...
}
用法:
int foo(char, bool);
g(foo);
我从事的事情可以做到您想要的事情 - 但是,它仅限于定义一个函数呼叫运算符(lambdas符合此限制)的函数。它还可以在MSVC 2012 CTP中使用。
namespace detail {
////////////////////////////////////////////////////////////////////////////
//! Select between function pointer types
////////////////////////////////////////////////////////////////////////////
template <typename T>
struct callable_helper_ptr;
//! non-member functions
template <typename R, typename... Args>
struct callable_helper_ptr<R (*)(Args...)> {
typedef void object_t;
typedef R result_t;
typedef std::tuple<Args...> args_t;
};
//! member functions
template <typename R, typename O, typename... Args>
struct callable_helper_ptr<R (O::*)(Args...)> {
typedef O object_t;
typedef R result_t;
typedef std::tuple<Args...> args_t;
};
//! const member functions
template <typename R, typename O, typename... Args>
struct callable_helper_ptr<R (O::*)(Args...) const> {
typedef O object_t;
typedef R result_t;
typedef std::tuple<Args...> args_t;
};
////////////////////////////////////////////////////////////////////////////
//! Select between function pointers and functors
////////////////////////////////////////////////////////////////////////////
template <typename T, typename is_ptr = typename std::is_pointer<T>::type>
struct callable_helper;
//! specialization for functors (and lambdas)
template <typename T>
struct callable_helper<T, std::false_type> {
typedef callable_helper_ptr<decltype(&T::operator())> type;
};
//! specialization for function pointers
template <typename T>
struct callable_helper<T, std::true_type> {
typedef callable_helper_ptr<T> type;
};
} //namespace detail
////////////////////////////////////////////////////////////////////////////////
//! defines the various details of a callable object T
////////////////////////////////////////////////////////////////////////////////
template <typename T>
struct callable_traits {
typedef typename detail::callable_helper<T>::type::object_t object_t;
typedef typename detail::callable_helper<T>::type::result_t result_t;
typedef typename detail::callable_helper<T>::type::args_t args_t;
template <unsigned N>
struct arg : public std::tuple_element<N, args_t> {};
};
,如果有人有兴趣,我就写了写作的过程:http://bkentel.wordpress.com/2012/12/12/defining-a-traits-type-for-callable-objects/
相关文章:
- 如何在模板参数中分离函数类型返回类型和参数
- 为什么要为指针返回类型返回一系列字符?
- 有没有办法根据运行时值的类型返回 constexpr 对象
- 从弱指针返回类型返回共享指针
- 无法为unique_ptr返回类型返回 nullptr
- 为什么双重类型返回无穷大
- std::is_arithmetic 为通用 lambda 中的 int 类型返回 false:未定义的行为?
- C 策略设计具有可变类型返回值
- 根据类型返回一个值
- 在C++中,将double转换为long的类型返回了不正确的值
- 作为宏参数的安装类型返回错误:字符串常量之前的预期非限定 id
- 具有引用类型返回的函数
- C++ 如何使模板<T>f() 为整数 T 返回 -1,为指针类型返回 nullptr
- C++模板:按类型返回值
- 以Integer返回类型返回浮点值
- 使用 C++14 的自动功能类型返回扣除代替 std::common_type 是否始终安全?
- 为什么允许新运算符向每个指针类型返回*void
- 适当的函数返回类型返回日期时间
- c++中布尔类型返回函数的默认返回值
- 在c#中使用非托管c++代码对所有double类型返回0