1

我正在编写 C++98(对不起),但是使用 C 库,它有许多对象存储在以下形式的数据结构中:

struct c_container
{
    size_t len;
    int data[1];
};

struct c_container *make_container(size_t n)
{
    if (n == 0)
        return NULL;
    struct c_container *rv = (struct c_container *)malloc(sizeof(rv->len) + n*sizeof(rv->data));
    rv->len = n;
    return rv;
}

我想使用 C++ 风格的迭代BOOST_FOREACH,但这不起作用。(手动调用 range_begin 和 range_end 函数的“旧式”确实有效)。

inline int *range_begin(c_container *c)
{
    return c ? &c->data[0] : NULL;
}
inline int *range_end(c_container *c)
{
    return c ? &c->data[c->len] : NULL;
}
inline const int *range_begin(const c_container *c)
{
    return c ? &c->data[0] : NULL;
}
inline const int *range_end(const c_container *c)
{
    return c ? &c->data[c->len] : NULL;
}

namespace boost
{
    template<>
    struct range_mutable_iterator<c_container *>
    {
        typedef int *type;
    };
    template<>
    struct range_const_iterator<c_container *>
    {
        typedef const int *type;
    };
}

int main()
{
    c_container *coll = make_container(3);
    coll->data[0] = 1;
    coll->data[1] = 42;
    coll->data[2] = -1;

    BOOST_FOREACH(int i, coll)
    {
        std::cout << i << std::endl;
    }
}

根据http://www.boost.org/doc/libs/1_65_1/doc/html/foreach/extensibility.html ,这就是所有必要的我已经用类测试了它)

但是,该示例使用了一个类,而我使用的是指向类的指针。根据我的调查,它似乎正在使用仅用于const char *和的代码路径const wchar_t *

In file included from boost-foreach.cpp:6:0:
/usr/include/boost/foreach.hpp: In function ‘bool boost::foreach_detail_::done(const boost::foreach_detail_::auto_any_base&, const boost::foreach_detail_::auto_any_base&, boost::foreach_detail_::type2type<T*, C>*) [with T = c_container, C = mpl_::bool_<false>, const boost::foreach_detail_::auto_any_base& = const boost::foreach_detail_::auto_any_base&]’:
boost-foreach.cpp:65:5:   instantiated from here
/usr/include/boost/foreach.hpp:749:57: error: no match for ‘operator!’ in ‘!* boost::foreach_detail_::auto_any_cast [with T = c_container*, C = mpl_::bool_<false>, typename boost::mpl::if_<C, const T, T>::type = c_container*, const boost::foreach_detail_::auto_any_base& = const boost::foreach_detail_::auto_any_base&](((const boost::foreach_detail_::auto_any_base&)((const boost::foreach_detail_::auto_any_base*)cur)))’
/usr/include/boost/foreach.hpp:749:57: note: candidate is: operator!(bool) <built-in>

是否有一些额外的提升特质可以专攻或什么?

4

1 回答 1

1

似乎很难为指针类型定义范围函数。但是您可以直接定义它们c_container。代码如下所示:

#include <cstdlib>
#include <iostream>
#include <boost/foreach.hpp>

struct c_container
{
    size_t len;
    int data[1];
};

struct c_container *make_container(size_t n)
{
    if (n == 0)
        return NULL;
    struct c_container *rv = (struct c_container *)malloc(sizeof(rv->len) + n * sizeof(rv->data));
    rv->len = n;
    return rv;
}

inline int *range_begin(c_container &c)
{
    return c.len > 0 ? &c.data[0] : NULL;
}
inline int *range_end(c_container &c)
{
    return c.len > 0 ? &c.data[c.len] : NULL;
}
inline const int *range_begin(const c_container &c)
{
    return c.len > 0 ? &c.data[0] : NULL;
}
inline const int *range_end(const c_container &c)
{
    return c.len > 0 ? &c.data[c.len] : NULL;
}

namespace boost
{
    template<>
    struct range_mutable_iterator<c_container>
    {
        typedef int *type;
    };
    template<>
    struct range_const_iterator<c_container>
    {
        typedef const int *type;
    };
}

#define MY_FOREACH(x, y) BOOST_FOREACH(x, *y)

int main()
{
    c_container *coll = make_container(3);
    coll->data[0] = 1;
    coll->data[1] = 42;
    coll->data[2] = -1;

    //BOOST_FOREACH(int i, *coll)
    MY_FOREACH(int i, coll)
    {
        std::cout << i << std::endl;
    }
}

请注意,BOOST_FOREACH循环不会迭代指针类型。作为一种解决方法,您可以定义自己的方法FOREACH,如上面的代码所示。

于 2017-10-30T09:54:14.593 回答