我正在尝试编译以下代码:
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/assign/std/vector.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/algorithm/copy.hpp>
int main( int argc, char ** argv )
{
using namespace boost::assign;
using boost::adaptors::indirected;
std::vector<boost::optional<unsigned> > values;
values += 1u,2u,3u;
boost::copy( values | indirected, std::ostream_iterator<unsigned>( std::cout, " " ) );
std::cout << std::endl;
}
但是,我遇到了一些错误,例如没有名为 element_type
的类型在 boost::optional<unsigned>
. reference page但是,页面说唯一的先决条件是 operator*()
的存在。一元函数。有没有办法让它发挥作用?
最佳答案
这绝对是 Boost 中的一个错误,但是这个错误是在 Boost.Optional 中还是在 Boost.Iterator 中还有待讨论(我个人认为是后者)。
但是,修复是微不足道的——在包含任何 Boost header 之前,请执行以下操作:
#include <boost/optional/optional_fwd.hpp>
#include <boost/pointee.hpp>
namespace boost
{
template<typename P>
struct pointee<optional<P> >
{
typedef typename optional<P>::value_type type;
};
}
然后根据需要包含其他 Boost header 。
请在Boost Trac上提交工单,或者至少在 Boost Users mailing list 上发布错误报告.
关于c++ - 将 boost::optional 与 boost::adaptors::indirected 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6123327/