Boost logo

Boost Users :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2006-08-24 04:56:23


> -----Original Message-----
> From: boost-users-bounces_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Arno Schödl

> I simply want to increment a macro, like that:
>
> #define VARIABLE 7
> FunctionA( VARIABLE ); // VARIABLE expands to 7
> #define VARIABLE BOOST_PP_INC(VARIABLE)
> FunctionB( VARIABLE ); // VARIABLE expands to 8
> #define VARIABLE BOOST_PP_INC(VARIABLE)
> FunctionC( VARIABLE ); // VARIABLE expands to 9
> #define VARIABLE BOOST_PP_INC(VARIABLE)
> ...
>
> Of course, as I have written it, it does not work. What's the
> right way?

The closest mechanism in the pp-lib is the slot mechanism. E.g.

#include <boost/preprocessor/slot/slot.hpp>

#define BOOST_PP_VALUE 7
#include BOOST_PP_ASSIGN_SLOT(1)

FunctionA( BOOST_PP_SLOT(1) );
    // BOOST_PP_SLOT(1) expands to 7

#define BOOST_PP_VALUE BOOST_PP_SLOT(1) + 1
#include BOOST_PP_ASSIGN_SLOT(1)

FunctionB( BOOST_PP_SLOT(1) );
    // BOOST_PP_SLOT(1) expands to 8

#define BOOST_PP_VALUE BOOST_PP_SLOT(1) + 1
#include BOOST_PP_ASSIGN_SLOT(1)

FunctionC( BOOST_PP_SLOT(1) );
    // BOOST_PP_SLOT(1) expands to 9

This can be condensed somewhat:

// ----- variable.hpp ----- //

#ifndef VARIABLE_HPP
#define VARIABLE_HPP

#include <boost/preprocessor/slot/slot.hpp>

#define BOOST_PP_VALUE 7
#include BOOST_PP_ASSIGN_SLOT(1)

#define VARIABLE BOOST_PP_SLOT(1)
#define INC_VARIABLE() "inc_variable.hpp"

#endif

// ----- inc_variable.hpp ----- //

#define BOOST_PP_VALUE VARIABLE + 1
#include BOOST_PP_ASSIGN_SLOT(1)

// ----- client.cpp ----- //

#include "variable.hpp"

FunctionA( VARIABLE );
#include INC_VARIABLE()
FunctionB( VARIABLE );
#include INC_VARIABLE()
FunctionC( VARIABLE );

That is about the best you can do.

Regards,
Paul Mensonides


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