接近STL算法,lambda,本地类和其他方法 [英] Approaching STL algorithms, lambda, local classes and other approaches

查看:151
本文介绍了接近STL算法,lambda,本地类和其他方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用STL似乎有必要的事情之一是指定本地函数的方法。
我通常提供的许多函数不能使用STL函数对象创建工具(例如bind)创建,我必须手动滚动我的函数对象。

One of the things that seems to be necessary with use of STL is a way to specify local functions. Many of the functions that I would normally provide cannot be created using STL function object creation tools ( eg bind ), I have to hand roll my function object.

因为C ++标准禁止本地类型在模板实例化中用作参数,所以我能够使用的最好的是创建一个小库(只显示相关部分)

Since the C++ standard forbids local types to be used as arguments in template instantiations the best I was able to use was to create a small library, ( just showing relevant parts )

// library header
class MyFunctionBase<R,T>  
{  
 public:  
   virtual ~MyFunctionBase();  
   virtual R operator()(const T &) const=0;  
};    


class  MyFunction<R,T> 
{   
    MyFunctionBase<R,T> *b; 
 public: 
    ~MyFunction()
    {
       delete b;
    }
    virtual R operator()(const T &) const
    {
        return (*b)(T);
    } 
};


// source file
....

    class func: public MyFunctionBase  ...
    std::stl_alg(....    MyFunction(new funct));

这对我来说总是不现实。我想对ISO委员会的人也这么认为,并添加了一个lambda到C ++。

This has always seemed unwieldly to me. I guess to the people on the ISO committee believe so too and added a lambda to C++.

同时,编译器如何解决这个问题? (特别是Windows编译器。)

In the meantime how have compilers addressed this problem? ( Especially Windows compilers. )

一个可能澄清一点的修正。
更改日志:
Nov 2
替换为澄清
由于C ++标准禁止本地类作为函数对象

A correction which might clarify a bit. Changelog: Nov 2 replaced to clarify Since the C++ standard forbids local classes as function objects

推荐答案

Boost.Bind,Boost.Function和Boost.Lambda是您的朋友。

Boost.Bind, Boost.Function, and Boost.Lambda are your friends.

这篇关于接近STL算法,lambda,本地类和其他方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆