接触 STL 算法、lambda、局部类和其他方法
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准方式是“函子” - 基本上是一个提供
operator()
的struct
例如:
因为它是一个 struct/类,所以您可以对任何像“binary_operator”之类的东西以及维护更高级函子的状态。
The standard way is a "functor" - basically, a
struct
that supplies anoperator()
For example:
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.
Boost.Bind、Boost.Function 和 Boost.Lambda 是您的朋友。
Boost.Bind, Boost.Function, and Boost.Lambda are your friends.
对于 C++0x,您可以使用 lambda(如您所提到的):
这已经在 MS Visual C++ 2010(当前在社区技术预览中)和 GCC 4.3.x(带有 -std=c++0x 编译器标志)中可用。 但是,如果没有 lambda,您只需要提供一个类型:
有些算法需要二元函数对象,而有些则需要一元函数对象。 请参阅供应商的 STL 文档,准确了解哪些算法需要二进制函数对象,哪些算法需要一元函数对象。
您可能还想研究的一件事是 TR1 中
bind
和function
的较新实现(基于 Boost.Bind 和 Boost.Function)。With C++0x you can use lambda's (as you mentioned):
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:
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
andfunction
in TR1 (based on Boost.Bind and Boost.Function).