gpt4 book ai didi

c++ - BOOST_FOREACH 可以定制为指针类型吗?

转载 作者:行者123 更新时间:2023-11-30 05:08:07 25 4
gpt4 key购买 nike

我正在编写 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;
}

我想使用 BOOST_FOREACH 进行 C++ 风格的迭代,但这行不通。 (手动调用 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,这就是应该的所有必要条件(我已经用类(class)测试过)

但是,该示例使用了类,而我使用的是指向类的指针。根据我的调查,它似乎正在使用仅适用于 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>

是否有一些额外的 boost 特性可以专精或其他什么?

最佳答案

似乎很难为指针 类型定义范围函数。但是您可以直接为 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,如上面的代码所示。

关于c++ - BOOST_FOREACH 可以定制为指针类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47008591/

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