Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 可以为指针类型自定义BOOST_FOREACH吗?_C++_Boost_Boost Foreach - Fatal编程技术网

C++ 可以为指针类型自定义BOOST_FOREACH吗?

C++ 可以为指针类型自定义BOOST_FOREACH吗?,c++,boost,boost-foreach,C++,Boost,Boost Foreach,我正在编写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(sizeo

我正在编写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\u FOREACH
进行C++风格的迭代,但这不起作用。(手动调用range_begin和range_end函数的“旧式”不起作用)


是否有一些额外的boost特性需要专门化或其他什么?

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

#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;
    }
}
#包括
#包括
#包括
结构c_容器
{
尺寸透镜;
int数据[1];
};
结构c_容器*制作容器(大小n)
{
如果(n==0)
返回NULL;
结构c_容器*rv=(结构c_容器*)malloc(sizeof(rv->len)+n*sizeof(rv->data));
rv->len=n;
返回rv;
}
内联整数*范围\u开始(c\u容器和c)
{
返回c.len>0?&c.data[0]:空;
}
内联整数*范围\u结束(c\u容器和c)
{
返回c.len>0?&c.data[c.len]:空;
}
内联常量int*范围\u开始(常量c\u容器和c)
{
返回c.len>0?&c.data[0]:空;
}
内联常量int*范围结束(常量c容器和c)
{
返回c.len>0?&c.data[c.len]:空;
}
名称空间提升
{
模板
结构范围可变迭代器
{
typedef int*类型;
};
模板
结构范围常量迭代器
{
typedef const int*类型;
};
}
#定义MY_FOREACH(x,y)BOOST_FOREACH(x,*y)
int main()
{
c_容器*coll=制造_容器(3);
coll->data[0]=1;
coll->data[1]=42;
coll->data[2]=-1;
//BOOST_FOREACH(inti,*coll)
MY_FOREACH(国际学院,学院)
{

std::cout对于表示空容器的
NULL
的情况,这将失败……但我认为使用自定义宏,我无论如何都可以将其包装到类中。
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>
#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;
    }
}