如何为函数类型使用模板参数?

莱特曼

我想实现distance(x,y,f)的是计算你要申请的次数函数fx获得y

例如,如果f = squaredistance(2, 256, square) == 3

我在这里使用的C ++代码改编自Stepanov和McJones的《编程元素》:

DistanceType(F) distance(Domain(F) x, Domain(F) y, Square<int> f) {
    typedef DistanceType(F) N;
    // Precondition: y is reachable from x under f
    N n(0);
    while(x != y) {
        x = f(x);
        n += 1;
    }
    return n;
}

Domain(F)DistanceType(F)#defineD移动。int

我决定将函子用作函数类型,并确定此Square<T>函数模板的层次结构:

template<typename T>
class Transformation {
    public:
        Transformation() {};
        virtual T operator() (T x) = 0;
};

template<typename T>
class Square : public Transformation<T> {
    public:
        virtual T operator() (T x) { return x * x; }
};

当我尝试该distance功能时,它可以工作:

#include <iostream>
using namespace std;
int main() {
    int x = 2;
    int y = 256;
    Square<int> f = Square<int>();
    int d = distance(x, y, f);
    cout << "the distance between " << x << " and " 
        << y << " is " << d << endl;

    return 0;
}

完整说明(使用g ++编译)

我的问题:

如何制作f模板参数的类型

我已经试过了:

template<typename F>
DistanceType(F) distance(Domain(F) x, Domain(F) y, F f) {
    typedef DistanceType(F) N;
    // Precondition: y is reachable from x under f
    N n(0);
    while(x != y) {
        x = f(x);
        n += 1;
    }
    return n;
}

并将其更改为:

typedef Square<int> F;
int d = distance<F>(x, y, f);

但是,当我编译时,出现此错误:

In file included from /usr/include/c++/5/bits/stl_algobase.h:65:0,
                from /usr/include/c++/5/bits/char_traits.h:39,
                from /usr/include/c++/5/ios:40,
                from /usr/include/c++/5/ostream:38,
                from /usr/include/c++/5/iostream:39,
                from transformations.cpp:35:
/usr/include/c++/5/bits/stl_iterator_base_types.h: In instantiation of ‘struct std::iterator_traits<Square<int> >’:
/usr/include/c++/5/bits/stl_iterator_base_funcs.h:114:5:   required by substitution of ‘template<class _InputIterator> typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator) [with _InputIterator = Square<int>]’
transformations.cpp:42:32:   required from here
/usr/include/c++/5/bits/stl_iterator_base_types.h:168:53: error: no type named ‘iterator_category’ in ‘class Square<int>’
    typedef typename _Iterator::iterator_category iterator_category;
                                                    ^
/usr/include/c++/5/bits/stl_iterator_base_types.h:169:53: error: no type named ‘value_type’ in ‘class Square<int>’
    typedef typename _Iterator::value_type        value_type;
                                                    ^
/usr/include/c++/5/bits/stl_iterator_base_types.h:170:53: error: no type named ‘difference_type’ in ‘class Square<int>’
    typedef typename _Iterator::difference_type   difference_type;
                                                    ^
/usr/include/c++/5/bits/stl_iterator_base_types.h:171:53: error: no type named ‘pointer’ in ‘class Square<int>’
    typedef typename _Iterator::pointer           pointer;
                                                    ^
/usr/include/c++/5/bits/stl_iterator_base_types.h:172:53: error: no type named ‘reference’ in ‘class Square<int>’
    typedef typename _Iterator::reference         reference;

而且我不明白这个错误。为什么不能将其Square<int>用作模板参数?

阿空加瓜

为什么不只有两个模板参数?

template <typename T, typename F>
unsigned long distance(T x, T y, F f)
{
    unsigned long n = 0;
    while(x != y)
    {
        x = f(x);
        ++n;
    }
    return n;
}

这也适用于函数指针...

接受功能的变体

template <typename T>
unsigned long distance(T x, T y, T (f)(T));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用模板参数键入函数指针

如何在类外的签名中使用模板参数的嵌套类型定义模板类函数

如何为各种模板类型构建哈希函数?

如何为const模板参数定义复制构造函数?

如何为函数参数指定通用类型

如何为函数提供纯类型参数?

如何为某些模板类型禁用类成员函数

使用派生的模板参数类型作为函数的返回类型(CRTP)?

使用函数的返回类型和参数类型作为模板类型

模板类型推导如何使用重载函数作为参数

从函数参数推断模板类型

如何为链接模板同时使用uri和传入参数?

如何为GHCI强制执行类型构造函数参数

如何为使用参数调用React setState的函数创建TypeScript类型?

如何使用模板类型作为函数参数派生抽象模板类(C ++ 11)

何时使用模板非类型参数VS。构造函数参数?

使用函数指针作为模板函数类型参数?

如何使用非类型模板参数和类型模板参数的混合来对函数进行模板化?

如何为模板函数重载[]

如何使用参数调用类模板函数

如何使用static_assert检查模板化函数的迭代器参数的元素类型?

如何为此JS函数使用参数?

如何创建返回类型的constexpr函数(将在模板参数中使用)

如何使用模板参数成员的类型?

如何使用模板模板类型作为函数参数?

如何使用仲裁类型的模板参数?

如何使用类型名和非类型名模板参数调用泛型函数?

如何使用模板模板参数类型的引用类型模板参数定义类模板

如何使用模板<typename... Args> 函数的扩展参数列表创建类型?