Boost logo

Boost Users :

Subject: Re: [Boost-users] preprocessor metaprogramming question
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-06-20 20:04:15


AMDG

Nathan Ridge wrote:
> Here's a question for the preprocessor metaprogramming experts out there.
>
> I have some classes that need to have write() methods that look like this:
>
> template <typename output>
> bool write(output& out) const
> {
> return utils::write(out, member_1) &&
> utils::write(out, member_2) &&
> ...
> utils::write(out, member_n);
>
> }
>
>
>
> where member_1, member_2, ..., member_n are the member variables of the class.
>

#include <boost/preprocessor/seq/for_each.hpp>

#define WRITE(r, data, member) && utils::write(out, member)

#define MEMBERS(members) \
    template<typename output> \
    bool write(output& out) const \
    { \
        return true \
            BOOST_PP_SEQ_FOR_EACH(WRITE, ~, members); \
    }

MEMBERS((a)(b)(c))

In Christ,
Steven Watanabe


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