Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 推断函数对象参数类型_C++_Templates_Type Deduction - Fatal编程技术网

C++ 推断函数对象参数类型

C++ 推断函数对象参数类型,c++,templates,type-deduction,C++,Templates,Type Deduction,给定一组参数类型和一个函数对象,可以使用decltype推导返回类型。推导参数类型怎么样 对于函数指针,返回类型和参数类型都可以通过有趣的模式匹配语法推导出来。例如,这里有一个愚蠢的程序,它使用推导的参数类型打印出一个字符串,描述函数指针的返回和参数类型 #include <iostream> #include <string> using std::string; template<class T> string type_str(); templat

给定一组参数类型和一个函数对象,可以使用
decltype
推导返回类型。推导参数类型怎么样

对于函数指针,返回类型和参数类型都可以通过有趣的模式匹配语法推导出来。例如,这里有一个愚蠢的程序,它使用推导的参数类型打印出一个字符串,描述函数指针的返回和参数类型

#include <iostream>
#include <string>

using std::string;

template<class T>
string type_str();

template<>
string type_str<int>() { return "int"; }

template<>
string type_str<char>() { return "char"; }

string arg_types_str()
{
    return "";
}

template<class T>
string arg_types_str()
{
    return type_str<T>();
}

template<class T, class U, class... Args>
string arg_types_str()
{
    return type_str<T>() + ", " + arg_types_str<U, Args...>();
}

template<class R, class... Args>
void print_fptr_type(R (*fptr)(Args...))
{
    std::cout << type_str<R>() << " (*)(" << arg_types_str<Args...>() << ")" << std::endl;
}

int main()
{
    int (*fptr)(char, int);
    print_fptr_type(fptr);
}

是否可以编写一个类似于示例的程序来打印函数对象的返回和参数类型


对于只有一个
操作符()的函数对象来说,理论上可以明确地推断参数类型。

是的,可以这样做。该策略类似,但对于功能对象,它是一个两阶段的过程。首先,获取指向
操作符()
的成员函数指针,然后可以使用类似的模式匹配方法进行类型推断

新计划的关键部分包括:

template<class T, class R, class... Args>
void print_memfptr_types(R (T::*memfptr)(Args...))
{
    std::cout << type_str<R>() << " (" << type_str<T>() << "*)(" << arg_types_str<Args...>() << ")" << std::endl;
}

template<class T, class R, class... Args>
void print_cmemfptr_types(R (T::*memfptr)(Args...))
{
    std::cout << type_str<R>() << " (const " << type_str<T>() << "*)(" << arg_types_str<Args...>() << ")" << std::endl;
}

template<class T>
void print_fnc_obj_types(T&)
{
    print_memfptr_types(&T::operator());
}

template<class T>
void print_fnc_obj_types(const T&)
{
    print_cmemfptr_types(&T::operator());
}
模板
无效打印类型(R(T::*memfptr)(Args…)
{
标准::cout
template<class T, class R, class... Args>
void print_memfptr_types(R (T::*memfptr)(Args...))
{
    std::cout << type_str<R>() << " (" << type_str<T>() << "*)(" << arg_types_str<Args...>() << ")" << std::endl;
}

template<class T, class R, class... Args>
void print_cmemfptr_types(R (T::*memfptr)(Args...))
{
    std::cout << type_str<R>() << " (const " << type_str<T>() << "*)(" << arg_types_str<Args...>() << ")" << std::endl;
}

template<class T>
void print_fnc_obj_types(T&)
{
    print_memfptr_types(&T::operator());
}

template<class T>
void print_fnc_obj_types(const T&)
{
    print_cmemfptr_types(&T::operator());
}