C++ Variadic Macro To Specify Multiple Conditions -


I suspect that I could not do this, but I thought I would ask the wise community first here.

I have to see that in any handful (they say that ten, although perhaps only two or three) the variables are equal to the same specific value as

  If (x == 3 || y == 3 || z == 3 || w == 3) ...   

In Python I am just doing if In 3 (X, Y, Z, W): , but whatever.

However, I am wondering whether it is possible such essence EQU_ANY (3, X, Y, Z, W) instead of a bunch in a variadic macro < Code> write EQU_ANYX macro where X is the number of Arg. <>

[The solution to use a macro is finally, because it is sinister and yucky.]


If you are creating copies, then this is probably std :: find :

  std :: array & lt; Int, 4 & gt; Value = {x, y, z, w}; If (std :: find (values.begin (), values.end (), 3)! = Values.end ()) {}   

to wrap this line Very often a function:

  template & lt; Typename container, typed nim & gt; In Bool (const container and C, const value & amp; v) {return std :: find (c.begin (), c.end (), v)! = C.end (); }   

is used as:

  std :: arrays and lt; Int, 4 & gt; Value = {x, y, z, w}; If (C ((value, 3)) {}   

In C ++ 0x, you can use an initial list instead of creating a temporary array: < Pre> (if ({x, y, z, w}, 3) is included ()

(it works in GCC 4.5+; support me It is not known about any other compiler that has yet it is the C ++ 0x feature.)


If you really want to save the copy of objects (e.g., if they are large or Mim to mimic Nge are), you can use the passive version of the same function:

  #include & lt; boost / iterator / indirect_iterator.hpp & gt; template & lt; typename container, Taipanam moist & gt; in bool indirect_contains (const container & amp; C, the constant value & amp; V) {return std :: think (boost :: make_indirect_iterator (c.begin ()), boost :: make_indirect_iterator ( C.end ()), v) = Promotion:: make_indirect_iterator (c.end ());}   

is used as:

 < Code> std :: arrays and lt; Int *, 4 & gt; Value = {& amp; X, & amp; Y, & amp; Z, & amp; W}; If (indirect_contains (values, 3)) {}   

or with a C ++ 0x Starter list:

  if (indirect_contains ({& amp; the X, & amp; Y, & amp; Z, -shyam}, 3)) {}   

as mentioned by Jonathan Lefler Boost.Preprocessor later, here The solutions that will appear are:

  #include & lt; Boost / preprocessor.hpp & gt; #define SEQUENCE_CONTAINS_IMPL (R, data, I, ELEM) \ BOOST_PP_IIF (BOOST_PP_EQUAL (i, 0), BOOST_PP_EMPTY (), ||) \ ((ELEM) == (data)) #define SEQUENCE_CONTAINS (element value) \ ( BOOST_PP_SEQ_FOR_EACH_I (SEQUENCE_CONTAINS_IMPL, value, element)) used as   

:

  if (SEQUENCE_CONTAINS ((x) (y) (z) (w) ), 3)) {}   

How it is expanded:

  if ((x) == (3)) || (( Y) = = (3)) || ((z) == (3)) || ((w) == (3)))) {}   

(It's ugly And terrible; I will not use it in my code, but if you actually have two or three So Ulyon worried about copies, probably do not have a chance to call this function.)

Comments