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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:34:05+00:00 2026-05-17T19:34:05+00:00

I have a class with two definitions of ordering. (In the real problem, one

  • 0

I have a class with two definitions of ordering. (In the real problem, one is a total order and one is a semiorder.) But it’s nice to be able to use the comparison operators rather than always having to use an explicit comparison function or functor object. So I figured I’d provide some comparison operators like this:

class C;

namespace Order1 {
  bool operator< (const C&, const C&);
}
namespace Order2 {
  bool operator< (const C&, const C&);
}

Operators for >, <=, >= are also defined of course, but that’s not the point. Now a user can say using namespace Order1; or … Order2 at file scope or block scope and get the requested behavior for the rest of that file/block.

The disappointing part, which I’d like to improve if possible, is that these usings can’t nest.

void g(const C&, const C&);

using namespace Order1; // for most functions in this file

void f(const C& x, const C& y) {
  bool test1 = x < y; // Order1
  {
    // Would like to switch to Order2 for this block.
    using namespace Order2;
    bool test2 = x < y; // Ambiguous overload!

    g(x, y); // Unaffected by local using-s.
  }
}

Since using-directives don’t hide anything when used in the same namespace, this doesn’t provide a way to nicely temporarily reverse the meaning of the operators for a block scope.

Another related idea would be to allow dummy objects on the stack whose constructors and destructors manipulate the “current setting” for which behavior to use. But I don’t think I want to head that way for this situation, since that would mean the equivalent of f above could change the behavior of other functions called like g.

Is there another way to get a similar effect but allowing nested operations with the innermost block “hiding” the others? Or am I stuck with one behavior of overloaded operators per declarative region? Which I guess is manageable, since code can still explicitly use a function or functor instead of using the operators.

  • 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-17T19:34:06+00:00Added an answer on May 17, 2026 at 7:34 pm

    I would stick with ordinary comparison functions. The rest of the code will be cleaner. No using namespace... or explicit calls to scoped operator<. Reads easier this way, IMO…

    int main() {
       bool b = compare1(4, 5);
       b = compare2(4, 5);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class split across two files. One of these is generated, the
I have a really simple class with two methods; One that will be called
Let's say I have two models, Classes and People. A Class might have one
I have two classes, and want to include a static instance of one class
I have two class definitions in my Spring MVC web application named Class and
I have a code base, in which for Matrix class, these two definitions are
I have a Generic Class Factory class that has two methods one utilizes the
I have a class with two methods defined in it. public class Routines {
I have a class that contains two methods like these: public String getFoo(Int32 a)
I have an solution in VS 2008 which contains two class library projects and

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.