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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:11:19+00:00 2026-06-16T00:11:19+00:00

I have a class with an attribute of type std::string. I’d like to provide

  • 0

I have a class with an attribute of type std::string. I’d like to provide some comparison operator functions like <, >, ==, <=, and >= for the class by comparing the attribute.

My questions is that: any easy way or tool to

(1) just write one or two functions, such as the one for operator < (and ==), others can be automatically generated.

(2) or even simpler since the class comparison is depending on its attribute of type std::string whose comparison functions are already provided.

  • 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-06-16T00:11:20+00:00Added an answer on June 16, 2026 at 12:11 am

    Curiously recurring template pattern

    In this case you provide a simple base class which implements all needed operators and simply inherit from it:

    template <class T>
    struct implement_relational_operators{
        friend bool operator<=(const T & a,const T & b){ return a < b || a == b; }
        friend bool operator>(const T &  a, const T & b){ return !(a <= b); }
        friend bool operator!=(const T &  a, const T & b){ return !(a == b);}
        friend bool operator>=(const T &  a, const T & b){ return !(a < b); }
    };
    
    template <class T>
    struct scalar : public implement_relational_operators<scalar<T> >{
        T value;
        bool operator<(const scalar& o) const { return value < o.value;}
        bool operator==(const scalar& o) const { return value == o.value;}
    };
    

    This doesn’t share the drawbacks from std::rel_ops (see below). However, you still need to implement operator< and operator==. A similar trick is used by boost.

    C++20’s <=> might improve the situation further (see below).

    std::rel_ops (deprecated in C++20)

    std::rel_ops provides the additional operations based on < an ==, so you just need to write two operators.

    However, it will be deprecated in C++20, where a <=> b will get thrown into the mix.

    Requirements

    You just need the equality (operator=) and the lesser than (operator<) comparison operator. The rest can be generated automatically with std::rel_ops, since the following holds:

    a != b equal to !(a == b)
    a <= b equal to (a < b) || (a == b)
    a >= b equal to !(a < b)
    a >  b equal to !(a <= b)
    

    Note that these are less efficient than writing those by hand.

    Warning

    However, since it’s easy to provide those operators yourself you should just take the extra effort and write them. And they also have some drawbacks, as mentioned by R. Martinho Fernandes:

    Note that [they] won’t work properly unless you:

    1. add using namespace std::rel_ops on every context you use the operators; or
    2. add using::operator!=; using::operator<=; using::operator>=; using::operator>; in the namespace of your class (using namespace std::rel_ops in the namespace of your class is not acceptable because it does not get picked up by ADL).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If say I have some generic class, for example: public class Attribute<T> { }
I have custom attribute defined like so: [AttributeUsage(AttributeTargets.Field)] public class EnumDisplayAttribute : Attribute {
I have class and properties in there. Some properties can be marked attribute (it's
I have a class Klass with a class attribute my_list . I have a
I have multiple images that have the same class attribute and I want to
I have a class with an attribute and getter method: public Class MyClass {
Let's say I have an attribute class: public class MyCustomAttribute : Attribute { //
class a : b = 10 I have a class 'a' with attribute 'b'.
I have a class which has a private attribute which is a reference to
I have a class with an NSDictionary attribute. Inside this class I dispatch another

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.