使用模板元编程执行过载解决
Perform overload resolution with template meta-programming
受另一个问题的启发,我试图找到一种推导类型的方法给定用于调用的实际参数的重载成员函数该功能。以下是我目前所拥有的:
#include <type_traits>
template<typename F, typename Arg>
struct mem_fun_type {
// perform overload resolution here
typedef decltype(std::declval<F>()(std::declval<Arg>())) result_type;
typedef decltype(static_cast<result_type (F::*)(Arg)>(&F::operator())) type;
};
struct foo {};
struct takes_two
{
void operator()(int);
void operator()(foo);
};
struct take_one {
void operator()(float);
};
int main()
{
static_assert(std::is_same<mem_fun_type<take_one, float>::type,
void (take_one::*)(float)>::value, "Zonk");
static_assert(std::is_same<mem_fun_type<takes_two, double>::type,
void (takes_two::*)(float)>::value, "Zonk");
return 0;
}
只要模板参数Arg与实际类型匹配static_cast会成功,但这只是过载分辨率(精确匹配)。是否可以执行完成模板元编程中的过载解决过程?
这纯粹是假设性的,不适用于现实世界。
这是我迄今为止最接近的方法:定义不同大小的函数返回表,结果是sizeof(select(...))
接收指向要匹配的函数的指针。为了确保即使给定类中不存在函数,代码也会编译,可以使用单独的检查has_function
。
过载解决的结果在select<has_function<T>::value, T>::value
中。
有了这段代码,您甚至可以"解析"数据成员,而不仅仅是函数,这只是为select函数设置正确参数的问题。
然而,这里有一个不足——重载解析不在函数参数上,而是在函数类型上。这意味着不会发生通常的参数类型转换。
// Verify the name is valid
template <typename T>
struct has_function
{
struct F {int function;};
struct D : T, F {};
template <typename U, U> struct same_;
template <typename C> static char(&select_(same_<int F::*, &C::function>*))[1];
template <typename> static char(&select_(...))[2];
enum {value = sizeof(select_<D>(0)) == 2};
};
// Values to report overload results
enum type { none=1 , function_sz_size_t , function_sz , function_string };
template <bool, typename R> struct select;
template <typename R> struct select<false, R>
{
enum {value = none};
};
template <typename R> struct select<true, R>
{
// Define your overloads here, they don't have to be templates.
template <typename Ret, typename Arg> static char(&select_(Ret (R::*)(const char*, Arg)))[function_sz_size_t];
template <typename Ret, typename Arg> static char(&select_(Ret (R::*)(Arg)))[function_sz];
template <typename Ret> static char(&select_(Ret (R::*)(std::string)))[function_string];
template <typename Ret> static char(&select_(Ret (R::*)(std::string&&)))[function_string];
template <typename Ret> static char(&select_(Ret (R::*)(const std::string&)))[function_string];
static char(&select_(...))[none];
enum {value = sizeof(select_(&R::function))};
};
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 运行同一解决方案的另一个项目的项目
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何解决 Ninja c++ 构建和执行问题
- 无法在问题解决方案中执行输出逻辑
- 解决方案在第 25 行执行错误'out of bounds'
- 自上而下的动态规划与递归朴素解决方案.检查运行时执行
- 如何在解决方案中生成多个项目之前执行流程
- R6010 执行后出错.if 语句在 for 循环中.[作业][中止已解决,发现新问题]
- 解决rdtsc的解决方案执行
- Qt - 解决一个插槽上的两个顺序调用,并且仅执行一次操作
- 在VS2015社区版(C++)中从项目/解决方案中创建可执行文件
- 使用模板元编程执行过载解决
- 如何将c++可执行代码扩展到不同的代码层(例如产品代码、解决方案代码)
- VS2015可执行文件成为病毒(有潜在的解决方案,但不知道为什么)
- 我只能想到蛮力解决方案.如何执行递归