gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:26 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

一种常用的变体是使用 EBO(空基类优化)来减小大小。您不能“调整”标准库对来执行相同的操作。

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 = 元编程库。元编程处理编译时“值”——它们将被编码为类型¹

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

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

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

,Boost Fusion 确实std::pair改编为Fusion Sequence
#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 晚得多

关于c++ - boost::mpl 不使用(甚至不兼容)std::pair 的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com