9

I am trying to compile the following code:

#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;
}

However, I got some errors, e.g. that there is no type named element_type in boost::optional<unsigned>. The reference page page, however, says that the single precondition is the existence of the operator*() unary function. Is there a way to make it work?

manlio
  • 18,345
  • 14
  • 76
  • 126
Mathias Soeken
  • 1,293
  • 2
  • 8
  • 22

2 Answers2

7

This is definitely a bug in Boost, but whether that bug is in Boost.Optional or Boost.Iterator is up for debate (I would say the latter, personally).

However, the fix is trivial -- before including any Boost headers, do this:

#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;
    };
}

Then include other Boost headers as necessary.

Please submit a ticket on the Boost Trac, or at the least post a bug report on the Boost Users mailing list.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • include header using is better. – Akira Takahashi May 26 '11 at 00:59
  • @Akira : Good point; I forgot it was there since it's not in the root `boost` directory unlike most other Boost libs' forward declaration headers. – ildjarn May 26 '11 at 01:23
  • 1
    Rediscovering bugs that no-one bothered to report when told to is awesome. I independently found and reproduced this, resulting in this [trac ticket](https://svn.boost.org/trac/boost/ticket/5811). – Lars Viklund Aug 30 '11 at 15:31
  • @Lars: awesome indeed. I just did the same. However, as I quoted in a just posted answer this is probably by design: http://stackoverflow.com/questions/7254131/is-it-possible-to-use-boostfilter-iterator-for-output/7254598#7254598 – sehe Aug 31 '11 at 09:05
1

Look at the private optional.hpp defined in boost iostreams library here. You will see that it defines a typedef T element_type;

However the actual optional.hpp that you are using defined here does not define it. So that is why the compiler is complaining. I don't know why it was overlooked.

Try using the private optional.hpp from iostreams library to solve this issue. I hope this helps.

O.C.
  • 6,711
  • 1
  • 25
  • 26
  • 2
    Using classes/functions inside `detail` namespaces should **always** be a last resort; they're put there because they're specifically not intended for use outside of the given library's implementation. – ildjarn May 26 '11 at 01:45