接触 STL 算法、lambda、局部类和其他方法

发布于 2024-07-08 18:28:34 字数 853 浏览 6 评论 0原文

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

由于 C++ 标准禁止将本地类型用作模板实例化中的参数,因此我能使用的最好方法是创建一个小型库(仅显示相关部分),

// 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 委员会的人也这么认为,并在 C++ 中添加了 lambda。

与此同时,编译器如何解决这个问题? (尤其是 Windows 编译器。)

可能会澄清一点的更正。 变更日志: 11 月 2 日 替换以澄清 由于 C++ 标准禁止局部类作为函数对象

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.

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));

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++.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-07-15 18:28:34

标准方式是“函子” - 基本上是一个提供 operator()struct

例如:

struct MyMinFunctor {
  bool operator()(const int& a, const int& b) { return a > b; }
};

vector<int> v;
sort(v.begin(), v.end(), MyMinFunctor());

因为它是一个 struct/类,所以您可以对任何像“binary_operator”之类的东西以及维护更高级函子的状态。

The standard way is a "functor" - basically, a struct that supplies an operator()

For example:

struct MyMinFunctor {
  bool operator()(const int& a, const int& b) { return a > b; }
};

vector<int> v;
sort(v.begin(), v.end(), MyMinFunctor());

Because it is a struct/class, you can subclass any of the things like 'binary_operator' as well as maintain state for more advanced functors.

被翻牌 2024-07-15 18:28:34

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

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

夜血缘 2024-07-15 18:28:34

对于 C++0x,您可以使用 lambda(如您所提到的):

for_each(container.begin(), container.end(),
  [](auto item) {
    // do something with item
  }
  );

这已经在 MS Visual C++ 2010(当前在社区技术预览中)和 GCC 4.3.x(带有 -std=c++0x 编译器标志)中可用。 但是,如果没有 lambda,您只需要提供一个类型:

  1. 可默认构造
  2. 可复制构造
  3. 定义函数运算符重载

有些算法需要二元函数对象,而有些则需要一元函数对象。 请参阅供应商的 STL 文档,准确了解哪些算法需要二进制函数对象,哪些算法需要一元函数对象。

您可能还想研究的一件事是 TR1 中 bindfunction 的较新实现(基于 Boost.Bind 和 Boost.Function)。

With C++0x you can use lambda's (as you mentioned):

for_each(container.begin(), container.end(),
  [](auto item) {
    // do something with item
  }
  );

This is already available in MS Visual C++ 2010 (currently in Community Tech Preview) and GCC 4.3.x (with the -std=c++0x compiler flag). However, without lambda's, you just need to provide a type that:

  1. Is default constructible
  2. Is copy constructible
  3. Defines a function operator overload

There are some algorithms that require binary function objects while there are some that require unary function objects. Refer your vendor's STL documentation to find out exactly which algorithms require binary function objects and which ones require unary function objects.

One thing you might also want to look into are the newer implementations of bind and function in TR1 (based on Boost.Bind and Boost.Function).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文