Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4086972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:47:46+00:00 2026-05-20T18:47:46+00:00

I suspect that I can’t do this, but figured I’d ask the wise community

  • 0

I suspect that I can’t do this, but figured I’d ask the wise community here first.

I want to check if any of a handful (say ten, though probably just two or three) of variables are equal to the same specific value. e.g.

if (X == 3 || Y == 3 || Z == 3 || W == 3) ...

In Python I’m used to simply doing if 3 in (X, Y, Z, W):, but whatever.

Anyway, I’m wondering if it’s possible to abstract it into a variadic macro such as EQU_ANY(3, X, Y, Z, W) instead of writing a bunch of EQU_ANYX macros where X is the number of args.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:47:47+00:00Added an answer on May 20, 2026 at 6:47 pm

    [The solution using a macro is at the end, because it’s rather horrible and yucky.]


    If you don’t mind copies being made, it’s probably cleaner to use std::find:

    std::array<int, 4> values = { X, Y, Z, W };
    if (std::find(values.begin(), values.end(), 3) != values.end()) { }
    

    It’s often nice to wrap this line into a function:

    template <typename Container, typename Value>
    bool contains(const Container& c, const Value& v) 
    {
        return std::find(c.begin(), c.end(), v) != c.end();
    }
    

    used as:

    std::array<int, 4> values = { X, Y, Z, W };
    if (contains(values, 3)) { }
    

    In C++0x, you can use an initializer list instead of creating a temporary array:

    if (contains({ X, Y, Z, W }, 3)) { }
    

    (This works in gcc 4.5+; I’m not aware of any other compilers that support this C++0x feature yet.)


    If you really want to avoid copying the objects (e.g., if they are large or expensive to copy), you can use an indirect version of the same function:

    #include <boost/iterator/indirect_iterator.hpp>
    
    template <typename Container, typename Value>
    bool indirect_contains(const Container& c, const Value& v)
    {
        return std::find(boost::make_indirect_iterator(c.begin()),
                         boost::make_indirect_iterator(c.end()),
                         v) 
            != boost::make_indirect_iterator(c.end());
    }
    

    Used as:

    std::array<int*, 4> values = { &X, &Y, &Z, &W };
    if (indirect_contains(values, 3)) { }
    

    Or, with a C++0x initializer list:

    if (indirect_contains({ &X, &Y, &Z, &W }, 3)) { }
    

    Since Jonathan Leffler mentioned Boost.Preprocessor, here is what that solution would look like:

    #include <boost/preprocessor.hpp>
    
    #define SEQUENCE_CONTAINS_IMPL(r, data, i, elem)                          \
        BOOST_PP_IIF(BOOST_PP_EQUAL(i, 0), BOOST_PP_EMPTY(), ||)              \
        ((elem) == (data))
    
    
    #define SEQUENCE_CONTAINS(elements, value)                                \
        (BOOST_PP_SEQ_FOR_EACH_I(SEQUENCE_CONTAINS_IMPL, value, elements))
    

    Used as:

    if (SEQUENCE_CONTAINS((X)(Y)(Z)(W), 3)) { }
    

    Which expands to:

    if ((((X) == (3)) || 
         ((Y) == (3)) || 
         ((Z) == (3)) || 
         ((W) == (3)))) { }
    

    (This is ugly and horrible; I wouldn’t use this in my code, but if you’re really worried about copies of two or three values being made, you probably don’t want to chance a function call being made either.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suspect that one of my applications eats more CPU cycles than I want
I didn't find any option for that, so I suspect that some views follow
I suspect this applies to general ASP.Net too but I am not sure. If
I use numpy for numerical linear algebra. I suspect that I can get much
I've read over this question and I think that is a great start, but
How to represent the space inside this statement: C:\\Program Files so that I can
I have a loop that can look like this: For Each article In artAll
I suspect that calls from separate threads (>15) are having a negative effect on
I suspect that adding a certain letter/character to the beginning of my text will
My company has a problem: we suspect that the NACHA files we are receiving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.