Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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_C++03_Type Deduction - Fatal编程技术网

C++ 推断函数指针或函子的返回类型

C++ 推断函数指针或函子的返回类型,c++,templates,c++03,type-deduction,C++,Templates,C++03,Type Deduction,我试图推导一个可调用类型的返回类型,即函数指针或函子 我之前问过,得到了一个答案,说明了如何对函数指针执行此操作,并给出了如何对函子执行此操作的提示 基于此,我有了这个helper结构,它现在可以用于函子,但不再适用于函数指针 // functors template<class T> struct Return; { typedef typename T::result_type type; }; // function pointers template<cla

我试图推导一个可调用类型的返回类型,即函数指针或函子

我之前问过,得到了一个答案,说明了如何对函数指针执行此操作,并给出了如何对函子执行此操作的提示

基于此,我有了这个helper结构,它现在可以用于函子,但不再适用于函数指针

// functors
template<class T>
struct Return;
{
    typedef typename T::result_type type;
};

// function pointers
template<class R>
struct Return<R (*)()>
{
        typedef R type;
};
我很困惑为什么这不是一个SFINAE错误,它会继续并抓住
返回的专门化

你可以自己尝试一下,也可以看到一些我正在做的事情的背景

上下文

这是基于这个问题

在这个问题中,我试图创建一个函数模板,通过使用传递的映射函数将std::vector映射到新的std::vector。现在我尝试从一个容器映射,该容器提供了一种迭代其内容的方法。在对该问题的回答中,函数模板的形式如下:

template<typename F, typename T>
typename expr_check<sizeof(declval<F>()(declval<T>())), std::vector<T> >::type
map_vec(F fnc, const std::vector<T>& source)

这是重复数据消除人员发现的愚蠢错误

工作示例:

问题只是需要专门化
Return
,以适合将要使用的函数签名

// functors
template<class T>
struct Return
{
    typedef typename T::result_type type;
};

// function pointers
template<class R, class T>
struct Return<R (*)(T)>
{
        typedef R type;
};

// function pointers
template<class R, class T, class U>
struct Return<R (*)(T, U)>
{
        typedef R type;
};
//函子
模板
结构返回
{
typedef typename T::result_type type;
};
//函数指针
模板
结构返回
{
typedef-R型;
};
//函数指针
模板
结构返回
{
typedef-R型;
};

您的错误只针对零个参数

由于pre-C++11没有可变模板,您必须为每个计数添加一个专门化,直到达到您的限制


如果您可以使用C++11功能,只需使用。

您的错误只针对零个参数。你想专门处理任意参数,我才意识到!那真是愚蠢透顶,我太轻易放弃了。
template<typename F, typename C>
typename expr_check<sizeof(declval<F>()(declval<int>())),
        std::vector<typename Return<F>::type> >::type
map_vec(F fnc, const C& source);
// functors
template<class T>
struct Return
{
    typedef typename T::result_type type;
};

// function pointers
template<class R, class T>
struct Return<R (*)(T)>
{
        typedef R type;
};

// function pointers
template<class R, class T, class U>
struct Return<R (*)(T, U)>
{
        typedef R type;
};