boost::mpl不使用(甚至不兼容)std::pair的原因是什么?

What is the reason why boost::mpl doesn't use (and is even not compatible with) std::pair?

在一个关于 boost::mpl::map 的简单问题 How to get *any* example of boost::mpl::map working? 上花了很多时间后,我不明白为什么 boost 在他们的 MPL 实现中不接受 std::pair。我知道,他们可能会遗漏标准对中的点点滴滴,但他们肯定可以包含适应 std::pair.

的代码

毕竟std::pair是语言的一部分。

这种现象并不局限于std::pair。据我了解,std::tupleboost::mpl::vector 非常相似,但两种类型从不在 boost 库中互操作。

正如 How to get *any* example of boost::mpl::map working? 上的@lisyarus 所指出的,不同之处在于,与 std:: 版本相比,boost::mpl::pair 从未打算存储值。但我还是不明白——不存储一个值也意味着它必须保持正确的类型吗?如果我只关心类型,我仍然可以使用 std::pair 及其 ::first::second 成员并简单地丢弃运行时值。

but surely they could include a code that adapts to the std::pair

一个经常使用的变体是使用 EBO(空基类优化)来减小大小。你不能 "adapt" 标准库对做同样的事情。

As @lisyarus on How to get any example of boost::mpl::map working? pointed out, the difference is that in contrast to the std:: version, boost::mpl::pair was never meant to store a value.

正确。 MPL = 元编程库。元编程处理编译时 "values" - 将以类型编码¹

But I still don't understand it - doesn't storing a value also mean that it has to keep the proper type?

当然可以。然而,这并不是 Boost MPL 的设计目的。

如果您正在寻找 bridge 纯类型操作 运行时值的库,那么您很幸运:该库称为 Boost Fusion。而且,不出所料,如果您包含

,Boost Fusion 确实std::pair 调整为融合序列
#include <boost/fusion/adapted/std_pair.hpp>

If all I care about are types, I can still use std::pair and its ::first and ::second members and simply discard the runtime value.

是的,你可以。但是那时您不需要 Boost MPL。同样,要弥合它,请考虑使用 Boost Fusion。

Note Even more modern would be to use Boost Hana, which is like Boost Fusion and MPL combined but with C++11/14 style:

Hana is a header-only library for C++ metaprogramming suited for computations on both types and values. The functionality it provides is a superset of what is provided by the well established Boost.MPL and Boost.Fusion libraries. By leveraging C++11/14 implementation techniques and idioms, Hana boasts faster compilation times and runtime performance on par or better than previous metaprogramming libraries, while noticeably increasing the level of expressiveness in the process. Hana is easy to extend in a ad-hoc manner and it provides out-of-the-box inter-operation with Boost.Fusion, Boost.MPL and the standard library.


¹ 好吧,直到 constexpr 评估,但比 Boost MPL 晚得多 written/designed