you have a good solution here, but I wanted a macro, so it can be processed in preprocessor time and not in compile time. here is the probleme in a more clear way: (sorry for the bad english) Right now I using this: #define DECORATION(z,n,data) ,BOOST_PP_ARRAY_ELEM( 0, data ) :: BOOST_PP_ARRAY_ELEM( n, data ) #define PP_NARG(...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N()) #define PP_NARG_(...) PP_ARG_N(__VA_ARGS__) #define PP_ARG_N( \ _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, _11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \ _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, _51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62,_63,N,...) N #define PP_RSEQ_N() \ 63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16, 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 #define FOREACH_ARRAY( ... ) (PP_NARG(__VA_ARGS__) , ( __VA_ARGS__ ) ) #define FOREACH( Name, A, ... ) BOOST_PP_REPEAT_FROM_TO(1, PP_NARG(Name, __VA_ARGS__), A, FOREACH_ARRAY(Name, __VA_ARGS__) ) #define EXCEPTION_ERRORS(enumName, ...) \ enum class _##enumName : unsigned { Default, __VA_ARGS__ }; \ typedef sp_enum<_##enumName FOREACH( _##enumName, DECORATION, Default, __VA_ARGS__) > enumName; So with this code, when I wrote EXCEPTION_ERRORS(My_Exception, Error1, Error2, Error3); It's translated in enum class _My_Exception:unsigned{Default, Error1, Error2, Error3); typedef sp_enum<_My_Exception, _My_Exception::Error1, _My_Exception::Error2, _My_Exception::Error3> My_Exception; with sp_enum a variadic template class: template<typename T, T head, T... values> class sp_enum<T, head, values...> : public sp_enum<T, values...>{ protected: static const unsigned _count = sp_enum<T, values...>::_count+1; static vector<T> _data; public: sp_enum( ) : sp_enum<T, values...>(values...) {_data.push_back(head);for(auto i= sp_enum<T, values...>::_data.begin();i != sp_enum<T, values...>::_data.end();++i){_data.push_back(*i);}} sp_enum(T v ) {_data.push_back(v );} sp_enum(T v, T...) : sp_enum<T, values...>(values...) {_data.push_back(v );for(auto i= sp_enum<T, values...>::_data.begin();i != sp_enum<T, values...>::_data.end();++i){_data.push_back(*i);}} vector<T> data() { return _data ;} unsigned count() { return _count ;} bool operator< (const sp_enum<T, values...>&) const {return false;} static T randomEnum() { srand(time(NULL));return _data[rand()%_count];} }; template<typename T, T head, T... values> vector<T> sp_enum<T, head, values...>::_data; This works just fine, but the dependency to this bizarre macro PP_NARG is irritating and frustrating. I just need a way to figure out the number of argument passed in. On Mon, Apr 26, 2010 at 11:46 PM, Kalle Olavi Niemitalo <kon@xxxxxx> wrote: > chedi toueiti <chedi.toueiti@xxxxxxxxx> writes: > >> I'm trying to write a macro to process a variable argument list but >> need to know exactly the number of argument supplied. >> Is there any way to do so with gcc ?? > > If all the args are convertible to the same type, you can do > something like this: > > #define COUNT_ARGS(...) (sizeof (char []) { __VA_ARGS__ }) > > Curiously, if I do "return COUNT_ARGS(42, 69, 105, 491);", > gcc-4.4.3 -Wall -Wextra does not warn that the last number > does not fit in char. > > With C++, overloading the comma operator seems like a solution: > > #include <stdio.h> > > template <int C> > struct count_args > { > char n[C]; > template <typename A> count_args <C + 1> operator , (A) const; > }; > > #define COUNT_ARGS(...) (sizeof (count_args <0> (), __VA_ARGS__).n) > > int main() > { > return COUNT_ARGS("hobby", -55, printf); > } > > I suspect that argument packs and constexpr in C++0x make it > possible to write this in a nicer way. >