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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:25:36+00:00 2026-05-25T16:25:36+00:00

So I have a std::vector<std::unique_ptr<Base>> vec and I’m trying to sort it dynamically, given

  • 0

So I have a std::vector<std::unique_ptr<Base>> vec and I’m trying to sort it dynamically, given that there are logical comparisons between Derived1 to Derivedn (Derivedn always > Derivedn-1 > … > Derived1) (say n = 10 or so) and each Derivedx has it’s own different comparison with Derivedx. As an example, think 10 digit integer > 9 digit integer > 1 digit integer, but within each derived class 53 > 32 (but I’m not sorting integers).

So I can do this:

std::sort(vec.begin(), vec.end(), 
[](std::unique_ptr<Base>& const a, std::unique_ptr<Base>& const b){
  return *a<*b;}

And then in Base, have a function Base::operator<(const Base& b) make comparisons if they are different Derived classes, and cast to Derivedx if they are the same with Derivedx::operator<(const Derivedx& d) if they are the same derived.

However, I would think that there’s a way that I can compare a to b automatically given the appropriate definitions in the derived classes, but I have been unable to implement it due to compile errors. I cannot get the lambda function to compare Derivedx < Derivedy dynamically.

I’ve tried Base::operator<(const std::unique_ptr<Base>) and then use return *a<b for a compiler error, saying that I used a deleted copy assignment operator (which I don’t understand, where is the assignment??). An abstract virtual Base::operator<(const Base& b) does practically the same thing I’m doing now with more work because I have to implement Derivedx::operator<(const Base& b) (for each Derivedx) and then cast down to (Derivedx) if they’re the same.

It may just be better that I compare everything in the base class rather than implementing n^2 comparisons in (n comparisons in n derived classes) though. But I do want to see if I can keep things “object oriented”.

Any thoughts on the design issue?
Thanks.

  • 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-25T16:25:36+00:00Added an answer on May 25, 2026 at 4:25 pm

    Take a look at Chapter 31, Making functions virtual with respect to more than one object in Scott Meyers, More Effective C++.

    Also, try googling on the phrases double dispatch and multiple dispatch.

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

Sidebar

Related Questions

I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
I have a std::vector<std::pair<int,std::pair<Bone,std::string> > > I'm trying to sort it with std sort,
I have several std::vector , all of the same length. I want to sort
I have a std::vector. I want to create iterators representing a slice of that
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I'm trying to do the following: I have: std::vector<std::vector<GLdouble[2]>> ThreadPts(4); then I try to
I have four std::vector containers that all might (or might not) contain elements. I
Here is what I'm trying to do. I have a std::vector with a certain
I have two containers, let's say they're defined like this: std::vector<std::unique_ptr<int>> a; std::vector<std::unique_ptr<int>> b;
I have a std::vector<unsigned short> that somehow contains numbers that are greater than 300.

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.