Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   boost::bind with boost::fusion::for_each (http://www.velocityreviews.com/forums/t954743-boost-bind-with-boost-fusion-for_each.html)

Mark 11-22-2012 04:14 AM

boost::bind with boost::fusion::for_each
 
Hi folks, given:

#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/vector.hpp>

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/next_prior.hpp>

#include <boost/fusion/mpl.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/array.hpp>

#include <boost/fusion/sequence/intrinsic/value_at.hpp>
#include <boost/fusion/include/value_at.hpp>

// boost::fusion::result_of::size
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/fusion/include/size.hpp>

// boost::fusion::at
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/include/at.hpp>

# include <boost/bind.hpp>

# include <iostream>
# include <vector>
# include <typeinfo>
# include <cstdio>

typedef std::vector < int > INT_VEC ;
typedef boost::array<int, 100> array_t;

struct my_struct {
int i;
bool j;
boost::array<int, 100> arr1;
INT_VEC vv ;
my_struct()
: vv ( 100 )
{}
};


BOOST_FUSION_ADAPT_STRUCT(
my_struct,
(int, i)
(bool, j)
(array_t, arr1 )
(INT_VEC, vv)
)

struct display_ {
typedef void result_type ;
template <typename T>
result_type operator()(T& t) const {
printf(" --%s-- \n", typeid(t).name());
parse(t);
}

void parse(int & f) { std::cout << f << std::endl; };
void parse(char & f) {};
void parse(bool & f) { std::cout << f << std::endl; };
void parse(array_t & f) {
for(int i = 0; i < 100; ++i)
std::cout << f[i] << std::endl;
}
void parse(INT_VEC & f) {
for(int i = 0; i < 100; ++i)
std::cout << f[i] << std::endl;
}
//another function...
};

class foo {
my_struct f;
display_ dt;
public :
foo()
{}
void work () {
boost::fusion::for_each(f, boost::bind ( display_(), &dt, _1 ) );
//dt.
}

};

int main() {

foo f;
f.work() ;

}

How do I invoke bind on display_::operator()?

boost::bind ( display_(), &dt, _1 ): produce the compiler (MSVC 2010) error:
'error C2780: 'display_::result_type display_::operator ()(T &) const' : expects 1 arguments - 2 provided'

Jeff Flinn 11-25-2012 06:17 PM

Re: boost::bind with boost::fusion::for_each
 
On 11/21/2012 11:14 PM, Mark wrote:
> Hi folks, given:
>
> #include <boost/fusion/include/sequence.hpp>
> #include <boost/fusion/include/algorithm.hpp>
> #include <boost/fusion/include/vector.hpp>
>
> #include <boost/fusion/include/adapt_struct.hpp>
> #include <boost/lexical_cast.hpp>
> #include <boost/mpl/eval_if.hpp>
> #include <boost/mpl/identity.hpp>
> #include <boost/mpl/next_prior.hpp>
>
> #include <boost/fusion/mpl.hpp>
> #include <boost/fusion/adapted.hpp>
> #include <boost/array.hpp>
>
> #include <boost/fusion/sequence/intrinsic/value_at.hpp>
> #include <boost/fusion/include/value_at.hpp>
>
> // boost::fusion::result_of::size
> #include <boost/fusion/sequence/intrinsic/size.hpp>
> #include <boost/fusion/include/size.hpp>
>
> // boost::fusion::at
> #include <boost/fusion/sequence/intrinsic/at.hpp>
> #include <boost/fusion/include/at.hpp>
>
> # include <boost/bind.hpp>
>
> # include <iostream>
> # include <vector>
> # include <typeinfo>
> # include <cstdio>
>
> typedef std::vector < int > INT_VEC ;
> typedef boost::array<int, 100> array_t;
>
> struct my_struct {
> int i;
> bool j;
> boost::array<int, 100> arr1;
> INT_VEC vv ;
> my_struct()
> : vv ( 100 )
> {}
> };
>
>
> BOOST_FUSION_ADAPT_STRUCT(
> my_struct,
> (int, i)
> (bool, j)
> (array_t, arr1 )
> (INT_VEC, vv)
> )
>
> struct display_ {
> typedef void result_type ;
> template <typename T>
> result_type operator()(T& t) const {
> printf(" --%s-- \n", typeid(t).name());
> parse(t);
> }
>
> void parse(int & f) { std::cout << f << std::endl; };
> void parse(char & f) {};
> void parse(bool & f) { std::cout << f << std::endl; };
> void parse(array_t & f) {
> for(int i = 0; i < 100; ++i)
> std::cout << f[i] << std::endl;
> }
> void parse(INT_VEC & f) {
> for(int i = 0; i < 100; ++i)
> std::cout << f[i] << std::endl;
> }
> //another function...
> };
>
> class foo {
> my_struct f;
> display_ dt;
> public :
> foo()
> {}
> void work () {
> boost::fusion::for_each(f, boost::bind ( display_(), &dt, _1 ) );
> //dt.
> }
>
> };
>
> int main() {
>
> foo f;
> f.work() ;
>
> }
>
> How do I invoke bind on display_::operator()?
>
> boost::bind ( display_(), &dt, _1 ): produce the compiler (MSVC 2010) error:
> 'error C2780: 'display_::result_type display_::operator ()(T &) const' : expects 1 arguments - 2 provided'
>


There shouldn't be a need for bind, since dt is a function object,
doesn't this work:

boost::fusion::for_each(f, dt );

Jeff



All times are GMT. The time now is 07:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.