0

I would like to know how can I create a c++ macro that would make an or between a given number of arguments, something like

#define aMacro(arg1,arg2,arg3,arg4) arg1==arg2 || arg1==arg3 || arg1==arg4

But with a varible number of arguments.

I know about varadic macros, I know they exist and I know there are some questions about it on SO but as far as I know none of them addresses my question on how to do the || part.

Thank you

Mppl
  • 941
  • 10
  • 18
  • Note that C++11 supports variadic macros with `__VA_ARGS__`. See also [Boost Preprocessor](http://www.boost.org/libs/preprocessor) for ways to handle loops on variable numbers of arguments. Also note that for safety, the body of the macro in the question should be `((arg1) == (arg2) || (arg1) == (arg3) || (arg1) == (arg4))` or you're vulnerable to being invoked with odd-ball expressions that completely mangle the meaning of your code. Parenthesize each argument in a macro expansion (until you know enough to know when it isn't 100% necessary). – Jonathan Leffler Sep 20 '13 at 06:08
  • 2
    tip: try to avoid macros if you can – AndersK Sep 20 '13 at 06:10
  • @SingerOfTheFall It's not a duplicate since the post you mentioned doesnt say how to make the or part. – Mppl Sep 20 '13 at 06:13
  • @claptrap why should I avoid it? I think in this case It will make my code more readable because it will replace a quite verbose expression that appears in my code many times. – Mppl Sep 20 '13 at 06:17
  • @Mppl because in C++ there is little reason to use macros when you have inline functions etc to your disposal. If you do not need to write portable code with conditional compilation then it can be avoided. – AndersK Sep 20 '13 at 06:46
  • Do people actually read questions before voting to close them as duplicates? Or do they simply scan for similar key words? – Benjamin Lindley Sep 20 '13 at 07:59

2 Answers2

1

From wikipedia, what you're looking for is a variadic macro:

A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments.

Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 (C99) revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 (C++11) revision of the C++ language standard.

So it is available in C++11 or in C99. It is available as en extension on some compiler too, for example GNU GCC supports it, and I think I read that VC++ too.

Then for the syntax, have a look at this post for example, which gives a straightforward example:

#define FOO(fmt, ...) printf(fmt, ##__VA_ARGS__)
Community
  • 1
  • 1
Jaffa
  • 12,442
  • 4
  • 49
  • 101
1

Hopefully, you're open to a vastly superior alternative to macros here that perhaps you weren't aware of. This uses variadic templates, which requires C++11. But if you want variadic macros, that requires C++11 as well.

template<typename T>
bool func(T const&)
{
    return false;
}

template<typename L, typename R, typename... Args>
bool func(L const& lhs, R const &rhs, Args const&... args)
{
    return (lhs == rhs) || func(lhs,args...);
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274