Boost logo

Boost Users :

From: Hossein Haeri (powerprogman_at_[hidden])
Date: 2006-05-05 06:45:26


Dear all,

A fragment of code of mine is like this:

template <class Container, size_t i>
struct print
{
  ostream& operator ()
    (const Container& c, ostream& out) const
  {
    if(out)
    {
      typedef typename Container::value_type
        value_type;
      typedef print<value_type, i - 1> NewPrint;
      NewPrint newPrint;
      
      for(typename Container::const_iterator
            j = c.begin(); j != c.end(); ++j)
        newPrint(*j, out);
      /*for_each(c.begin(),
                 c.end(),
                 bind(newPrint, _2, out)); //Error!*/
    }
    return out;
  }
};

AFAICS, there should be no harm in replacing that for
with the for_each in the comments -- the one is marked
with "//Error" I mean, except that "out" which is of
type ostream& is not Copy-Constructible, and therefore
_2 cannot be used against it. Anybody having a
workarounds for that? Doesn't Boost have anything
analog to boost::ref which can give me a
Copy-Constructible beast?

For your reference, you can find the whole program and
the compiler error message in the P.S.

TIA,
--Hossein

P.S.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <boost/bind.hpp>
#include <boost/mem_fn.hpp>

using std::ostream;
using std::cin;
using std::cout;
using std::copy;
using std::for_each;
using std::ostream_iterator;
using std::vector;
using boost::bind;
using boost::mem_fn;

template <class Container, size_t i>
struct print
{
  ostream& operator () (const Container& c, ostream&
out) const
  {
    if(out)
    {
      typedef typename Container::value_type
value_type;
      typedef print<value_type, i - 1> NewPrint;
      NewPrint newPrint;
      /*
      for(typename Container::const_iterator j =
c.begin(); j != c.end(); ++j)
        newPrint(*j, out);
      */
      for_each(c.begin(), c.end(), bind(newPrint, _2,
out)); //Error!
    }
    return out;
  }
};

template <class Container>
struct print<Container, 1>
{
  ostream& operator () (const Container& c, ostream&
out) const
  {
    if(out)
      copy(c.begin(),
           c.end(),
           ostream_iterator<typename
Container::value_type>(out, " "));
    return out;
  }
  ostream& DoIt (const Container& c, ostream& out)
const
  {
    if(out)
      copy(c.begin(),
           c.end(),
           ostream_iterator<typename
Container::value_type>(out, " "));
    return out;
  }
};

int main()
{
  int a1[] = {1, 2, 3, 4};
  const size_t n(sizeof(a1)/sizeof(*a1));
  
  vector<int> v(a1, a1 + n);
  print<vector<int>, 1> () (v, cout);

  int a2[n] = {5, 6, 7, 8};
  vector<vector<int> > m;
  m.push_back(v);
  m.push_back(vector<int>(a2, a2 + n));
  print<vector<vector<int> >, 2> () (m, cout);
  
  cin.get();
  return 0;
}

Compiler: Default compiler
Executing g++.exe...
g++.exe "E:\Print.cpp" -o "E:\Print.exe"
-I"C:\Dev-Cpp\include\c++"
-I"C:\Dev-Cpp\include\c++\mingw32"
-I"C:\Dev-Cpp\include\c++\backward"
-I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:/Dev-Cpp/include/c++/3.3.1/bits/ios_base.h: In copy
constructor `
   std::basic_ios<char, std::char_traits<char>
>::basic_ios(const
   std::basic_ios<char, std::char_traits<char> >&)':
E:/Print.cpp:33: instantiated from `std::ostream&
print<Container, i>::operator()(const Container&,
std::ostream&) const [with Container =
std::vector<std::vector<int, std::allocator<int> >,
std::allocator<std::vector<int, std::allocator<int> >
> >, unsigned int i = 2]'
E:/Print.cpp:72: instantiated from here
C:/Dev-Cpp/include/c++/3.3.1/bits/ios_base.h:668:
error: `
   std::ios_base::ios_base(const std::ios_base&)' is
private
E:/Print.cpp:33: error: within this context

E:/Print.cpp: In member function `std::ostream&
print<Container,
   i>::operator()(const Container&, std::ostream&)
const [with Container =

   std::vector<std::vector<int, std::allocator<int> >,

   std::allocator<std::vector<int, std::allocator<int>
> > >, unsigned int i =
   2]':
E:/Print.cpp:72: instantiated from here
E:/Print.cpp:33: error: initializing argument 3 of `
   boost::_bi::bind_t<boost::_bi::unspecified, F,
typename
   boost::_bi::list_av_2<A1, A2>::type> boost::bind(F,
A1, A2) [with F =
   print<std::vector<int, std::allocator<int> >, 1>,
A1 = boost::arg<2>, A2 =
   std::basic_ostream<char, std::char_traits<char> >]'

C:/Dev-Cpp/include/boost/bind.hpp: At global scope:
C:/Dev-Cpp/include/boost/bind.hpp: In instantiation of
`boost::_bi::result_traits<boost::_bi::unspecified,
print<std::vector<int, std::allocator<int> >, 1> >':
C:/Dev-Cpp/include/boost/bind/bind_template.hpp:16:
instantiated from
`boost::_bi::bind_t<boost::_bi::unspecified,
print<std::vector<int, std::allocator<int> >, 1>,
boost::_bi::list2<boost::arg<2>,
boost::_bi::value<std::basic_ostream<char,
std::char_traits<char> > > > >'
E:/Print.cpp:33: instantiated from `std::ostream&
print<Container, i>::operator()(const Container&,
std::ostream&) const [with Container =
std::vector<std::vector<int, std::allocator<int> >,
std::allocator<std::vector<int, std::allocator<int> >
> >, unsigned int i = 2]'
E:/Print.cpp:72: instantiated from here
C:/Dev-Cpp/include/boost/bind.hpp:62: error: no type
named `result_type' in `
   struct print<std::vector<int, std::allocator<int>
>, 1>'
C:/Dev-Cpp/include/boost/bind/bind_template.hpp: In
instantiation of
`boost::_bi::bind_t<boost::_bi::unspecified,
print<std::vector<int, std::allocator<int> >, 1>,
boost::_bi::list2<boost::arg<2>,
boost::_bi::value<std::basic_ostream<char,
std::char_traits<char> > > > >':
E:/Print.cpp:33: instantiated from `std::ostream&
print<Container, i>::operator()(const Container&,
std::ostream&) const [with Container =
std::vector<std::vector<int, std::allocator<int> >,
std::allocator<std::vector<int, std::allocator<int> >
> >, unsigned int i = 2]'
E:/Print.cpp:72: instantiated from here
C:/Dev-Cpp/include/boost/bind/bind_template.hpp:16:
error: no type named `type'
   in `struct
boost::_bi::result_traits<boost::_bi::unspecified,
   print<std::vector<int, std::allocator<int> >, 1> >'

C:/Dev-Cpp/include/boost/bind/bind_template.hpp:19:
error: no type named `type'
   in `struct
boost::_bi::result_traits<boost::_bi::unspecified,
   print<std::vector<int, std::allocator<int> >, 1> >'
C:/Dev-Cpp/include/boost/bind/bind_template.hpp:25:
error: no type named `type'
   in `struct
boost::_bi::result_traits<boost::_bi::unspecified,
   print<std::vector<int, std::allocator<int> >, 1> >'
C:/Dev-Cpp/include/boost/bind/bind_template.hpp:31:
error: no type named `type'
   in `struct
boost::_bi::result_traits<boost::_bi::unspecified,
   print<std::vector<int, std::allocator<int> >, 1> >'
C:/Dev-Cpp/include/boost/bind/bind_template.hpp:31:
confused by earlier errors, bailing out

Execution terminated

                
___________________________________________________________
Switch an email account to Yahoo! Mail, you could win FIFA World Cup tickets. http://uk.mail.yahoo.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net