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 708925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:26:47+00:00 2026-05-14T04:26:47+00:00

motivation: I would like to create a utility class so that instead of having

  • 0

motivation: I would like to create a utility class so that instead of having to write:

if( someVal ==  val1 || someVal == val2 || someVal == val3 )

I could instead write:

if( is(someVal).in(val1, val2, val3) )

which is much closer to the mathematical ‘a is an element of (b,c,d)’ and also would save on a lot of typing when the variable name ‘someVal’ is long.

Here is the code I have so far (for 2 and 3 values):

template<class T>
class is {
private:
    T t_;
public:
    is(T t) : t_(t) { }

    bool in(const T& v1, const T& v2) { 
        return t_ == v1 || t_ == v2; 
    }
    bool in(const T& v1, const T& v2, const T& v3) { 
        return t_ == v1 || t_ == v2 || t_ == v3; 
    }
};

However it fails to compile if I write:

is(1).in(3,4,5);

instead I have to write

is<int>(1).in(3,4,5);

Which isn’t too bad, but it would be better if somehow the compiler could figure out that the type is int with out me having to explicitly specify it.
Is there anyway to do this or I am stuck with specifying it explicitly?

  • 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-14T04:26:47+00:00Added an answer on May 14, 2026 at 4:26 am

    If you want to keep this syntax, you can use a helper function like :

    template<class T>
    class is_op {
    private:
        T t_;
    public:
        is_op(T t) : t_(t) { }
    
        bool in(const T& v1, const T& v2) { 
            return t_ == v1 || t_ == v2; 
        }
        bool in(const T& v1, const T& v2, const T& v3) { 
            return t_ == v1 || t_ == v2 || t_ == v3; 
        }
    };
    
    
    template< class U >
    inline is_op<U> is( U const& v )
    {
        return is_op<U>( v );
    }
    
    int main(int argc, char* argv[])
    {
        is( 1 ).in( 1 , 2 , 4 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a virtual drive for windows. I'm not looking to
I would like any advice on how to create and visualize a link map
I would like to see a small but complete snippet of code that will
Motivation: I would like to convert hashes (MD5/SHA1 etc) into decimal integers for the
Motivation I'd like to find a way to take an arbitrary color and lighten
I'd like to hear what is the motivation behind the bulk of sealed classes
I have a service that accepts callbacks from a provider. Motivation : I do
I would like to do some fairly heavy-duty reflection in Ruby. I want to
I have a a hash foo = {'bar'=>'baz'} I would like to call foo.bar
I'm struggling to create a generic (or untyped) array in C (I'm aware that

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.