You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boost MSM状态机超50转换:扩展mpl::vector至60的实现疑问

扩展Boost MPL Vector容量至60的问题与解决

背景

Boost MSM状态机的转换表基于mpl::vector实现时,默认最大容量为20。通过定义以下宏可将上限提升至50:

#define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
#define BOOST_MPL_LIMIT_VECTOR_SIZE 50                
#define BOOST_MPL_LIMIT_MAP_SIZE 50 

要进一步扩展至60,需添加mpl/vector60.hpp和mpl/map60.hpp文件。现有boost/mpl/vector目录下存在vector50.hpp和vector50_c.hpp,其中vector50.hpp内容如下:

#ifndef BOOST_MPL_VECTOR_VECTOR50_HPP_INCLUDED
#define BOOST_MPL_VECTOR_VECTOR50_HPP_INCLUDED

// Copyright Aleksey Gurtovoy 2000-2004
//
// Distributed under the Boost Software License, Version 1.0. 
// (See accompanying file LICENSE_1_0.txt or copy at 
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/mpl for documentation.

// $Id$
// $Date$
// $Revision$

#if !defined(BOOST_MPL_PREPROCESSED_HEADERS)
#   include <boost/mpl/vector/vector40.hpp>
#endif

#include <boost/mpl/aux_/config/use_preprocessed.hpp>

#if !defined(BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS) \
    && !defined(BOOST_MPL_PREPROCESSING_MODE)

#   define BOOST_MPL_PREPROCESSED_HEADER vector50.hpp
#   include <boost/mpl/vector/aux_/include_preprocessed.hpp>

#else

#   include <boost/mpl/aux_/config/typeof.hpp>
#   include <boost/mpl/aux_/config/ctps.hpp>
#   include <boost/preprocessor/iterate.hpp>

namespace boost { namespace mpl {

#   define BOOST_PP_ITERATION_PARAMS_1 \
    (3,(41, 50, <boost/mpl/vector/aux_/numbered.hpp>))
#   include BOOST_PP_ITERATE()

}}

#endif // BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS

#endif // BOOST_MPL_VECTOR_VECTOR50_HPP_INCLUDED

疑问

  1. 是否需要同时添加vector60.hpp和vector60_c.hpp?二者区别是什么?
  2. 这些文件应放置在哪个目录下?是否为boost/mpl/vector?
  3. 如何修改文件内容?

尝试与错误

我自行编写了vector60.hpp初稿:

#ifndef BOOST_MPL_VECTOR_VECTOR60_HPP_INCLUDED
#define BOOST_MPL_VECTOR_VECTOR60_HPP_INCLUDED

// Copyright Aleksey Gurtovoy 2000-2004
//
// Distributed under the Boost Software License, Version 1.0. 
// (See accompanying file LICENSE_1_0.txt or copy at 
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/mpl for documentation.

// $Id$
// $Date$
// $Revision$

#if !defined(BOOST_MPL_PREPROCESSING_MODE)
#   include <boost/mpl/vector/vector50.hpp>
#endif

#include <boost/mpl/aux_/config/use_preprocessed.hpp>

#if !defined(BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS) \
    && !defined(BOOST_MPL_PREPROCESSING_MODE)

#   define BOOST_MPL_PREPROCESSED_HEADER vector60.hpp
#   include <boost/mpl/vector/aux_/include_preprocessed.hpp>

#else

#   include <boost/mpl/aux_/config/typeof.hpp>
#   include <boost/mpl/aux_/config/ctps.hpp>
#   include <boost/preprocessor/iterate.hpp>

namespace boost { namespace mpl {

#   define BOOST_PP_ITERATION_PARAMS_1 \
    (3,(51, 60, <boost/mpl/vector/aux_/numbered.hpp>))
#   include BOOST_PP_ITERATE()

}}

#endif // BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS

#endif // BOOST_MPL_VECTOR_VECTOR60_HPP_INCLUDED

但运行以下测试代码时:

#define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
#define BOOST_MPL_LIMIT_VECTOR_SIZE 60
#include <boost/mpl/vector.hpp>

int main() {

    typedef boost::mpl::vector<
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int,
                int
            > vector_51;

    return 0;
}

出现编译错误:

/usr/include/boost/mpl/vector.hpp:36:1: fatal error: boost/mpl/vector/vector60.hpp: No such file or directory
   36 | #   include BOOST_PP_STRINGIZE(boost/mpl/vector/AUX778076_VECTOR_HEADER)

我偏好通过在包含头文件前添加代码实现编译时生成的方案,但目前先实现自定义文件让测试代码运行即可。

解答

1. 是否需要同时添加vector60.hpp和vector60_c.hpp?二者区别是什么?

  • 若仅使用mpl::vector(存储任意C++类型的容器),仅需添加vector60.hpp;若使用mpl::vector_c(存储同类型整数常量的容器,如vector_c<int, 1,2,3>),则需同时添加vector60_c.hpp。
  • 核心区别:vector.hpp系列是类型容器,用于存放任意C++类型;vector_c.hpp系列是整数常量容器,基于integral_c实现,专门用于管理同类型整数常量集合。

2. 文件放置目录

是的,必须将vector60.hpp和vector60_c.hpp放在boost/mpl/vector/目录下,与vector50.hpp、vector50_c.hpp同目录。本次编译错误的直接原因就是文件未放置在正确路径。

3. 文件内容修改

(1)vector60.hpp的正确内容

你编写的初稿已基本正确,无需额外修改:

  • 前置头文件引用vector50.hpp正确
  • 预处理器迭代范围(51, 60, ...)正确
  • 预编译头定义BOOST_MPL_PREPROCESSED_HEADER设为vector60.hpp正确

(2)vector60_c.hpp的编写(若需要)

参照vector50_c.hpp结构,复制后修改关键部分:

#ifndef BOOST_MPL_VECTOR_VECTOR60_C_HPP_INCLUDED
#define BOOST_MPL_VECTOR_VECTOR60_C_HPP_INCLUDED

// Copyright Aleksey Gurtovoy 2000-2004
//
// Distributed under the Boost Software License, Version 1.0. 
// (See accompanying file LICENSE_1_0.txt or copy at 
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/mpl for documentation.

// $Id$
// $Date$
// $Revision$

#if !defined(BOOST_MPL_PREPROCESSING_MODE)
#   include <boost/mpl/vector/vector50_c.hpp>
#endif

#include <boost/mpl/aux_/config/use_preprocessed.hpp>

#if !defined(BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS) \
    && !defined(BOOST_MPL_PREPROCESSING_MODE)

#   define BOOST_MPL_PREPROCESSED_HEADER vector60_c.hpp
#   include <boost/mpl/vector/aux_/include_preprocessed.hpp>

#else

#   include <boost/mpl/aux_/config/typeof.hpp>
#   include <boost/mpl/aux_/config/ctps.hpp>
#   include <boost/preprocessor/iterate.hpp>

namespace boost { namespace mpl {

#   define BOOST_PP_ITERATION_PARAMS_1 \
    (3,(51, 60, <boost/mpl/vector/aux_/numbered_c.hpp>))
#   include BOOST_PP_ITERATE()

}}

#endif // BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS

#endif // BOOST_MPL_VECTOR_VECTOR60_C_HPP_INCLUDED

(3)编译时生成方案(你的偏好)

要实现编译时自动生成而无需手动添加文件,需满足:

  1. 确保定义BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS(测试代码中已定义)
  2. 确保Boost预处理器工具链正常工作
  3. 无需手动创建vector60.hpp,但需保证编译器能找到boost/mpl/vector/aux_/numbered.hpp(该文件默认已存在)

内容的提问来源于stack exchange,提问作者Darragh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:25:25