Boost fusion using transform in for_each

I'm trying to build a small C++ example using boost fusion. However, Visual Studio 2013 gives me build errors for the following piece of code. It should simply go over a associative struct and print all member names to the console:

#include <iostream>
#include <type_traits>
#include <boost/fusion/adapted/struct/define_assoc_struct.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/algorithm/transformation/zip.hpp>
#include <boost/fusion/algorithm/transformation/transform.hpp>

namespace keys
{
     struct name
     {};
    struct id
     {};
}

BOOST_FUSION_DEFINE_ASSOC_STRUCT((), Student,
    (std::string, name, keys::name)
    (int, id, keys::id)
);

struct getnames
{
    template<typename Sig>
    struct result;

    template <typename S, typename T>
    struct result<getnames(S, T)>
    {
        typedef std::string type;
    };

    template<class Struct, class N>
    typename result<getnames(Struct, N)>::type operator() (const N& i) const
    {
        return boost::fusion::extension::struct_member_name<Struct, i>::call();
    }
};

struct print
{
    template<typename Sig>
    struct result;

    template <typename T>
    struct result<print(T)>
    {
        typedef void type;
    };

    template<class S>
    void operator() (const S& i) const
    {
        std::cout << i << std::endl;
    };
};

int main()
{
    Student j = {"John", 42};
    auto names = boost::fusion::transform(j, getnames());
    boost::fusion::for_each(names, print());
    return 0;
}

This is my error: boost/fusion/view/transform_view/detail/deref_impl.hpp(38): error C2039: 'type' : is not a member of 'boost::mpl::apply<boost::fusion::detail::apply_transform_result<getnames>,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,boost::mpl::na,boost::mpl::na,boost::mpl::na,boost::mpl::na>' and four more errors which are coming up because of the first one.

To be honest, I am not an expert in the usage of boost fusion so maybe I simply missed something important here and someone else can help me.

Answers


There are several problems with your code.

1) In the Functor getnames, the signature of the result type and the signature of operator() are inconsistent (one takes one argument, the other takes two).

2) in the operator()(const N& i), i is a runtime variable. It cannot appear as a template parameter in the expression boost::fusion::extension::struct_member_name<Struct, i>.

I am not sure how to help without knowing what you want to do with getnames. Try to get to a consistent code first.


Need Your Help

How do you overwrite an inline javascript event using jQuery?

jquery events click

I just came across this weird problem that is happening in IE. In the html I am adding an onclick event to an element that calls a function to change a page like this,

What is causing the grey line?

javascript jquery html css

I have an image slideshow on my site: http://dev.jmret.com/recruit.php