C++:您使用 Loki 还是 Boost 作为函子吗?
我一直在阅读 Alexandrescu 的书《现代 C++ 设计》,他使用的技术给我留下了深刻的印象,所以我想添加 Loki 库 添加到我的应用程序。
然而,经过进一步调查,我发现我已经在使用的 boost 提供了很多类似的功能(但并非全部,例如我在 boost 中找不到单例),
我最感兴趣的是使用 loki 因为基于策略的设计和函子。
对我来说,boost 和 loki 各有利弊。我对 loki 的主要担忧是糟糕的文档(该库不再与本书绑定),但在我看来,loki 在某些领域比 boost 更强大、更灵活(我可能在这一点上是错的
)选择使用 boost 或 loki 作为函子和策略,我想知道在现实生活中使用它们的人的意见。
有时,事情在纸上看起来非常好,但当你真正使用它们时却有一些缺点:)
I've been reading Alexandrescu's book, Modern C++ design , and I've been quite impressed by the techniques he uses, so I wanted to add Loki library to my application.
However, after further investigation, I saw that boost, that I'm already using, provides a lot of similar functionality (not all though, I couldn't find a singleton in boost for example)
I was mostly interested in using loki because of the policy based design and the functors.
To me both, boost and loki have pros and cons. The main concern I have with loki is the poor documentation (the library isn't tied to the book anymore) but it seems to me that loki is more powerful and flexible than boost in some areas (I might be wrong on that one)
Before choosing to use boost or loki for functors and policies, I'd like to know the opinion of people who use them in real life.
Sometimes things look very good on paper but have some drawbacks when you use them for real:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Alexandrescu 有非常有趣的想法(类型列表、基于策略的类模板等),但其中很多都在 boost 中得到了改进,并在更广泛的编译器中进行了可移植性和正确性的测试。
出于这些原因,我建议尽可能选择提升。也就是说,《现代 C++ 设计》仍然对 C++ 的灵活性提供了很多见解,并深入了解一个人的想法(非常好的)来解决许多常见的编程问题。
例如,基于策略的智能指针是一个非常巧妙的想法,但我们可以找到为什么 boost 作者选择不以这种方式实现shared_ptr和scoped_ptr:
如果您确实需要各种各样的智能指针,并且您和您的团队能够轻松地广泛使用模板参数化,那么基于策略的方法来实现智能指针可能适合您。然而,scoped_ptr和shared_ptr(以及weak_ptr)往往能相当彻底地完成这项工作。策略类的组合行为可能更适合于有多种有用组合的情况。
尽管如此,Alexandrescu 仍有一些有趣的提案尚未得到解决。例如,在编译器更好地实现移动构造函数或直到我们可以使用 C++0x 中的右值引用。他对于实现内存分配器也有一些非常有趣的想法。
至于这个问题,我们在商业项目中使用了 Mojo 所需的 Loki 部分,但在适当的时候主要使用了 boost。
Alexandrescu had very interesting ideas (type lists, policy-based class templates, etc) but a lot of them have been improved upon in boost along with being tested across a wider range of compilers for portability and correctness.
I'd recommend preferring boost whenever possible merely for these reasons. That said, Modern C++ Design still provides a lot of insight into the flexibility of C++ and a look into one person's mind (a very good one) to approach a lot of common programming problems.
For instance, policy-based smart pointers are a very neat idea, but we can find why the boost authors chose not to implement shared_ptr and scoped_ptr this way:
If you do need a wide variety of smart pointers and you and your team are comfortable working extensively with template parameterization, then a policy-based approach to implementing smart pointers might work for you. However, scoped_ptr and shared_ptr (along with weak_ptr) tend to do the job quite thoroughly. The combinatorial behavior of policy classes is probably better used for something for which there are a wide variety of useful combinations.
Nevertheless, there are still some interesting proposals from Alexandrescu that boost has not addressed. MOJO, for instance, is still genuinely useful until compilers do a better job implementing move constructors or until we can use rvalue references from C++0x. He also has some very interesting thoughts on implementing memory allocators.
As for the question, we use parts of Loki needed for mojo in our commercial project, but mostly boost when it's appropriate.
也许需要考虑的一件事是,Boost 库在接受过程中必须经过同行评审过程。当然,在那之后,我相信对所做的更改确实很少有监督,但至少在它们被接受之前进行了一些审查。洛基只是一个人的幻象。当然,Alexandrescu 相当不错,但仍然……这都是他的想法,没有比这更进一步的评论了。
One thing to maybe consider is that boost libraries have to go through a peer review process during acceptance. After that of course I believe there's really very little oversight into what changes go in, but at least there's some review before they're accepted. Loki is just one man's vision. Of course Alexandrescu is quite good, but still...it's all his ideas and there's no further review than that.
我在整个 C++ 环境中使用 Boost,就像标准库的扩展(使用 VC9 和 VC10)。
我并不在所有项目中都使用它。
我在个人项目(主要是游戏)中使用它,我可以完全控制依赖项。
我在一个大型游戏项目中使用 boost::function (以及 boost 中的其他几个库)。
Loki也不错,但我觉得没必要。我认为我正在考虑使用的库的唯一部分是 Singleton,但我使用的是目前足够好的自定义部分。
I'm using Boost in my whole C++ environnement like an extension to the Standard Library (with VC9 and VC10).
I don't use it on all projects.
I use it on personal projects (mostly games) where I got full control of what are the dependencies.
I'm using boost::function in a big game project (with several other libraries from boost).
Loki is good too but I didn't feel the need. I think the only part of the library I'm thinking of using is the Singleton but I'm using a custom one that is fine enough for the moment.
C++0x 是我用于快速函数对象的。
C++0x is what I'm using for quick function objects.