Boost logo

Boost :

Subject: [boost] Re : [mpl] is_macro_called
From: Pierre Morcello (pmorcell-cppfrance_at_[hidden])
Date: 2010-04-04 18:36:23


> De: Lorenzo Caminiti <lorcaminiti_at_[hidden]> > Objet: [boost] [mpl] is_macro_called > À: boost_at_[hidden] > Date: Samedi 3 avril 2010, 13h25 > Hello all: > > Is there a way I can check at compile-time and class-scope > if a macro > has been called and expanded (at least one time) from > within a member > function? > > For example: > >     #define X(...) ... // Some macro definition > -- the macro > parameters could be anything needed to implement > is_macro_called. > >     struct is_macro_X_called { ... }; // Some > implementation -- this > could be a template or something else. > > So that: > >     struct z { >         void f() { >             X(...); // Macro > called from within a member function. >         } > >         // Detect at class-scope and a > compile-time that the macro was called. >         static const bool > is_macro_X_called::value; // Evaluates to true. >     }; > > Instead: > >     struct z { >         void f() { >             // X(...); // > Macro not called. >         } > >         static const bool > is_macro_X_called::value; // Evaluates to false. >     }; > > I do not know how to program this. However, could for > example the > macro X() instantiate a template with some special template > parameter > that can then be inspected at compile-time by a > is_macro_called > metafunction? > > Thank you very much. > Lorenzo Hello, I don't know how to do that compile time without macros. I wonder if it is possible with mpl. Anyway, maybe the following is enough for your need? [code] #include <iostream> #include <boost/preprocessor/slot/counter.hpp> #include <boost/type_traits/is_same.hpp> #define MACRO_Example(X) \ std::cout<<X<<std::endl; struct X { // at the beginning of the class. template <int Y> class MacroWasUsedHelper{}; typedef MacroWasUsedHelper<BOOST_PP_COUNTER> Before; void writeMe() { // if you comment the 2 next lines, the counter will be 0. MACRO_Example("Me"); #include BOOST_PP_UPDATE_COUNTER() } // at the end of the class declaration typedef MacroWasUsedHelper<BOOST_PP_COUNTER> After; // now you just have to compare the 2 types "Before" and "After" as you wish. the following will be 'boost::true_type' only if it was not used. typedef boost::is_same<Before, After>::type undetected; }; [/code] The elements like "typedef MacroWasUsedHelper<BOOST_PP_COUNTER> After;" can be put into a macro easily. Only the "#include BOOST_PP_UPDATE_COUNTER()" seems necessary to remain (as long as we use pure C++). Here you got compile time evaluation. It was also possible to use directly functions returning constant (BOOST_PP_COUNTER) instead of template programming. Hope this helps, Best Regards, Pierre Morcello


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk