从(成员)函数推导类型
Deduce type from (member) function
有没有一种简单的方法来推导成员函数的"类型"?我想推导以下(成员)函数的类型:
struct Sample {
void func(int x) { ... }
};
void func(int x) { ... }
至以下类型(用于std::function
):
void(int)
我正在寻找一个支持变量计数(而不是varargs!)参数的解决方案。。。
EDIT-示例:
我正在寻找一个类似于decltype
的表达式,我们称之为functiontype
,它具有以下语义:
functiontype(Sample::func) <=> functiontype(::func) <=> void(int)
CCD_ 4应计算为与CCD_ 5兼容的类型。
这有帮助吗?
#include <type_traits>
#include <functional>
using namespace std;
struct A
{
void f(double) { }
};
void f(double) { }
template<typename T>
struct function_type { };
template<typename T, typename R, typename... Args>
struct function_type<R (T::*)(Args...)>
{
typedef function<R(Args...)> type;
};
template<typename R, typename... Args>
struct function_type<R(*)(Args...)>
{
typedef function<R(Args...)> type;
};
int main()
{
static_assert(
is_same<
function_type<decltype(&A::f)>::type,
function<void(double)>
>::value,
"Error"
);
static_assert(
is_same<
function_type<decltype(&f)>::type,
function<void(double)>
>::value,
"Error"
);
}
相关文章:
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 将C++子类成员函数(虚拟实现)传递给 C 类型函数指针
- C++ 这里有一个返回 (24) 的布尔返回类型函数
- 使用SFINAE来检测void返回类型函数的存在
- 使用此类型函数有什么优势
- 为什么此函数通过类型函数指针调用后,呼叫明智地行为
- 如何使用无类型函数指针调用C++成员函数
- 模板返回类型函数如何在C++中工作
- 具有通用类型函数的动态库[C ]
- 如何在返回类型函数模板的专用化中使用派生类型?( "couldn't infer template argument" )
- Bon appetit :从 int 返回类型函数在 main() 中打印字符串
- 对于需要其他模板参数的类型函数的部分模板专业化
- c++错误的参数类型-函数指针
- 延迟评估模板类型函数
- 在引用或指针返回类型函数上输入
- 具有指针数据类型的非类型函数模板参数
- STL中使用的C++自定义比较类型(函数谓词与较少结构)
- C++模板基类的非类型函数模板的 using 声明
- 字符串到类型函数,模板专用化使调用统一
- 自由类型函数可以接受 Unicode 文件名吗?