Different types for `std::sort` comparator in C++

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












When we provide a comparator function for std::sort, we use following overload of std::sort:



template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );


in which comparator function for std::sort should have the following syntax:



bool cmp(const Type1 &a, const Type2 &b);


But as you see a and b may have different types. cppreference says:




The types Type1 and Type2 must be such that an object of type RandomIt
can be dereferenced and then implicitly converted to both of them. ​




But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.



Is ti possible that someone provide a small example with different types for std::sort's comparator function?



UPDATE:



even though my question is answered, I found this situation interesting in multiple inheritance. Here a small code for it, but in reality I don't know if I ever need this (unless you want to sort your Chimeras based on parent traits lol):



#include<iostream> 
#include<vector>
#include<algorithm>
using namespace std;

class A

public:
int x;
;

class B

public:
int y;
;

class C: public B, public A // Note the order

public:
C(int v1, int v2) this->x = v1; this->y = v2;
;

bool cmp(const A& v1, const B& v2)

return v1.x < v2.y;


int main()

vector<C> data;
data.push_back(C(2,3));
data.push_back(C(1,2));
data.push_back(C(2,1));
data.push_back(C(2,5));
sort(data.begin(), data.end(), cmp);

for (auto val : data)
cout << val.x << " " << val.y << endl;











share|improve this question



















  • 2




    For example, small integer types can be implicitly converted to larger integer types (like short or int to long). That means you could have a container of short values and a comparator taking one int and one long argument. I see no reason for this though.
    – Some programmer dude
    1 hour ago







  • 2




    I don't think you can have two types in a single array (without clever trick). The quote simply means RandomIt::value_type may be convertible to several other types.
    – Nicky C
    1 hour ago














up vote
6
down vote

favorite
1












When we provide a comparator function for std::sort, we use following overload of std::sort:



template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );


in which comparator function for std::sort should have the following syntax:



bool cmp(const Type1 &a, const Type2 &b);


But as you see a and b may have different types. cppreference says:




The types Type1 and Type2 must be such that an object of type RandomIt
can be dereferenced and then implicitly converted to both of them. ​




But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.



Is ti possible that someone provide a small example with different types for std::sort's comparator function?



UPDATE:



even though my question is answered, I found this situation interesting in multiple inheritance. Here a small code for it, but in reality I don't know if I ever need this (unless you want to sort your Chimeras based on parent traits lol):



#include<iostream> 
#include<vector>
#include<algorithm>
using namespace std;

class A

public:
int x;
;

class B

public:
int y;
;

class C: public B, public A // Note the order

public:
C(int v1, int v2) this->x = v1; this->y = v2;
;

bool cmp(const A& v1, const B& v2)

return v1.x < v2.y;


int main()

vector<C> data;
data.push_back(C(2,3));
data.push_back(C(1,2));
data.push_back(C(2,1));
data.push_back(C(2,5));
sort(data.begin(), data.end(), cmp);

for (auto val : data)
cout << val.x << " " << val.y << endl;











share|improve this question



















  • 2




    For example, small integer types can be implicitly converted to larger integer types (like short or int to long). That means you could have a container of short values and a comparator taking one int and one long argument. I see no reason for this though.
    – Some programmer dude
    1 hour ago







  • 2




    I don't think you can have two types in a single array (without clever trick). The quote simply means RandomIt::value_type may be convertible to several other types.
    – Nicky C
    1 hour ago












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





When we provide a comparator function for std::sort, we use following overload of std::sort:



template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );


in which comparator function for std::sort should have the following syntax:



bool cmp(const Type1 &a, const Type2 &b);


But as you see a and b may have different types. cppreference says:




The types Type1 and Type2 must be such that an object of type RandomIt
can be dereferenced and then implicitly converted to both of them. ​




But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.



Is ti possible that someone provide a small example with different types for std::sort's comparator function?



UPDATE:



even though my question is answered, I found this situation interesting in multiple inheritance. Here a small code for it, but in reality I don't know if I ever need this (unless you want to sort your Chimeras based on parent traits lol):



#include<iostream> 
#include<vector>
#include<algorithm>
using namespace std;

class A

public:
int x;
;

class B

public:
int y;
;

class C: public B, public A // Note the order

public:
C(int v1, int v2) this->x = v1; this->y = v2;
;

bool cmp(const A& v1, const B& v2)

return v1.x < v2.y;


int main()

vector<C> data;
data.push_back(C(2,3));
data.push_back(C(1,2));
data.push_back(C(2,1));
data.push_back(C(2,5));
sort(data.begin(), data.end(), cmp);

for (auto val : data)
cout << val.x << " " << val.y << endl;











share|improve this question















When we provide a comparator function for std::sort, we use following overload of std::sort:



template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );


in which comparator function for std::sort should have the following syntax:



bool cmp(const Type1 &a, const Type2 &b);


But as you see a and b may have different types. cppreference says:




The types Type1 and Type2 must be such that an object of type RandomIt
can be dereferenced and then implicitly converted to both of them. ​




But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.



Is ti possible that someone provide a small example with different types for std::sort's comparator function?



UPDATE:



even though my question is answered, I found this situation interesting in multiple inheritance. Here a small code for it, but in reality I don't know if I ever need this (unless you want to sort your Chimeras based on parent traits lol):



#include<iostream> 
#include<vector>
#include<algorithm>
using namespace std;

class A

public:
int x;
;

class B

public:
int y;
;

class C: public B, public A // Note the order

public:
C(int v1, int v2) this->x = v1; this->y = v2;
;

bool cmp(const A& v1, const B& v2)

return v1.x < v2.y;


int main()

vector<C> data;
data.push_back(C(2,3));
data.push_back(C(1,2));
data.push_back(C(2,1));
data.push_back(C(2,5));
sort(data.begin(), data.end(), cmp);

for (auto val : data)
cout << val.x << " " << val.y << endl;








c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 24 mins ago

























asked 1 hour ago









Afshin

1,7931418




1,7931418







  • 2




    For example, small integer types can be implicitly converted to larger integer types (like short or int to long). That means you could have a container of short values and a comparator taking one int and one long argument. I see no reason for this though.
    – Some programmer dude
    1 hour ago







  • 2




    I don't think you can have two types in a single array (without clever trick). The quote simply means RandomIt::value_type may be convertible to several other types.
    – Nicky C
    1 hour ago












  • 2




    For example, small integer types can be implicitly converted to larger integer types (like short or int to long). That means you could have a container of short values and a comparator taking one int and one long argument. I see no reason for this though.
    – Some programmer dude
    1 hour ago







  • 2




    I don't think you can have two types in a single array (without clever trick). The quote simply means RandomIt::value_type may be convertible to several other types.
    – Nicky C
    1 hour ago







2




2




For example, small integer types can be implicitly converted to larger integer types (like short or int to long). That means you could have a container of short values and a comparator taking one int and one long argument. I see no reason for this though.
– Some programmer dude
1 hour ago





For example, small integer types can be implicitly converted to larger integer types (like short or int to long). That means you could have a container of short values and a comparator taking one int and one long argument. I see no reason for this though.
– Some programmer dude
1 hour ago





2




2




I don't think you can have two types in a single array (without clever trick). The quote simply means RandomIt::value_type may be convertible to several other types.
– Nicky C
1 hour ago




I don't think you can have two types in a single array (without clever trick). The quote simply means RandomIt::value_type may be convertible to several other types.
– Nicky C
1 hour ago












3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










Its not about what is stored in the array. It is about what the comparator function is. Take for example this:



struct Animal ;
struct Cat : Animal ;
struct Dog : Animal ;
struct Hound : Dog ;

bool cmp(const Animal &a, const Animal &b);


Even if you have a list of Dogs, Cats or Hounds you can still sort them with the function cmp because they are all implicitly convertible. ie.



std::vector<Hound> hounds;
... // fill hounds
std::sort(hounds.begin(), hounds.end(), cmp);


And you can even imagine cases where Type1 and Type2 are not the same, eg.:



bool cmp(const Animal &a, const Dog &b); // Special compare to make dogs at the
// front because dogs are better!
etc ...



The types Type1 (Animal) and Type2 (Dog) must be such that an object of type RandomIt (Hound) can be dereferenced and then implicitly converted to both of them. ​Which is true.







share|improve this answer






















  • but still both arguments of comparator function is Animal. I want to see when we can have 2 different types in comparator function as standard.
    – Afshin
    1 hour ago










  • @Afshin I have added an example of this.
    – Fantastic Mr Fox
    1 hour ago










  • @Afshin its not about restricting to the most typical use case only but to have maximum possible generality (allowing differnt types for the comparator) while still guaranteeing correctness (the types can be implicitly converted). It would be a major annoyance if you had a function that can compare the instances in the container, but you could not use it for sorting the container
    – user463035818
    57 mins ago











  • Hm....This is true...But after a little thinking, I thought maybe this can be used for sorting in multiple-inherience. But maybe i'm wrong.
    – Afshin
    49 mins ago










  • added a multiple inherience example to my port :)
    – Afshin
    36 mins ago

















up vote
8
down vote














But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.




You can't have two different types in an array. The comparator doesn't suggest it's possible. It's specified like that simply because:



  1. The code can be well formed when the types are not the same.

  2. Demanding the same type is a restriction that serves little to no purpose.

So the specification offers a looser contract than is "obvious", in order to help our code be more flexible if needed. As a toy example, say we have this comparator laying around:



auto cmp(int a, long b) -> bool return a < b; 


Why prevent us from using this perfectly legal (albeit silly) function to sort an array of integers?






share|improve this answer




















  • I found it possible with multiple inheritance as I added in my post. if your array objects has multiple inheritance, you can compare 2 different types of parent in your comparator. But I agree it will be silly in normal conditions.
    – Afshin
    33 mins ago











  • @Afshin - Yes, that's one example. The point I was trying to make is that even without an example in mind, the specification can be reasoned and defined loosely to allow for flexibility in general.
    – StoryTeller
    29 mins ago

















up vote
1
down vote














But I still cannot get it exactly how we can have 2 different types in a single array




You cannot have two different types in a single array.



An array can have objects of only single type. But that single type must be implicitly convertible to both argument types of cmp.




Is ti possible that someone provide a small example with different types for std::sort's comparator function?




Here you go:



int arr = 1, 2, 3, 0;
auto cmp = (const int &a, const long &b)
return a < b;
;
std::sort(std::begin(arr), std::end(arr), cmp);


Note the two different arguments of cmp. This is just a minimal example, which is technically correct, but admittedly nonsensical. Frankly, I've never encountered a case where it would be useful to have different types for the arguments of a comparison function.






share|improve this answer






















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52984999%2fdifferent-types-for-stdsort-comparator-in-c%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote



    accepted










    Its not about what is stored in the array. It is about what the comparator function is. Take for example this:



    struct Animal ;
    struct Cat : Animal ;
    struct Dog : Animal ;
    struct Hound : Dog ;

    bool cmp(const Animal &a, const Animal &b);


    Even if you have a list of Dogs, Cats or Hounds you can still sort them with the function cmp because they are all implicitly convertible. ie.



    std::vector<Hound> hounds;
    ... // fill hounds
    std::sort(hounds.begin(), hounds.end(), cmp);


    And you can even imagine cases where Type1 and Type2 are not the same, eg.:



    bool cmp(const Animal &a, const Dog &b); // Special compare to make dogs at the
    // front because dogs are better!
    etc ...



    The types Type1 (Animal) and Type2 (Dog) must be such that an object of type RandomIt (Hound) can be dereferenced and then implicitly converted to both of them. ​Which is true.







    share|improve this answer






















    • but still both arguments of comparator function is Animal. I want to see when we can have 2 different types in comparator function as standard.
      – Afshin
      1 hour ago










    • @Afshin I have added an example of this.
      – Fantastic Mr Fox
      1 hour ago










    • @Afshin its not about restricting to the most typical use case only but to have maximum possible generality (allowing differnt types for the comparator) while still guaranteeing correctness (the types can be implicitly converted). It would be a major annoyance if you had a function that can compare the instances in the container, but you could not use it for sorting the container
      – user463035818
      57 mins ago











    • Hm....This is true...But after a little thinking, I thought maybe this can be used for sorting in multiple-inherience. But maybe i'm wrong.
      – Afshin
      49 mins ago










    • added a multiple inherience example to my port :)
      – Afshin
      36 mins ago














    up vote
    7
    down vote



    accepted










    Its not about what is stored in the array. It is about what the comparator function is. Take for example this:



    struct Animal ;
    struct Cat : Animal ;
    struct Dog : Animal ;
    struct Hound : Dog ;

    bool cmp(const Animal &a, const Animal &b);


    Even if you have a list of Dogs, Cats or Hounds you can still sort them with the function cmp because they are all implicitly convertible. ie.



    std::vector<Hound> hounds;
    ... // fill hounds
    std::sort(hounds.begin(), hounds.end(), cmp);


    And you can even imagine cases where Type1 and Type2 are not the same, eg.:



    bool cmp(const Animal &a, const Dog &b); // Special compare to make dogs at the
    // front because dogs are better!
    etc ...



    The types Type1 (Animal) and Type2 (Dog) must be such that an object of type RandomIt (Hound) can be dereferenced and then implicitly converted to both of them. ​Which is true.







    share|improve this answer






















    • but still both arguments of comparator function is Animal. I want to see when we can have 2 different types in comparator function as standard.
      – Afshin
      1 hour ago










    • @Afshin I have added an example of this.
      – Fantastic Mr Fox
      1 hour ago










    • @Afshin its not about restricting to the most typical use case only but to have maximum possible generality (allowing differnt types for the comparator) while still guaranteeing correctness (the types can be implicitly converted). It would be a major annoyance if you had a function that can compare the instances in the container, but you could not use it for sorting the container
      – user463035818
      57 mins ago











    • Hm....This is true...But after a little thinking, I thought maybe this can be used for sorting in multiple-inherience. But maybe i'm wrong.
      – Afshin
      49 mins ago










    • added a multiple inherience example to my port :)
      – Afshin
      36 mins ago












    up vote
    7
    down vote



    accepted







    up vote
    7
    down vote



    accepted






    Its not about what is stored in the array. It is about what the comparator function is. Take for example this:



    struct Animal ;
    struct Cat : Animal ;
    struct Dog : Animal ;
    struct Hound : Dog ;

    bool cmp(const Animal &a, const Animal &b);


    Even if you have a list of Dogs, Cats or Hounds you can still sort them with the function cmp because they are all implicitly convertible. ie.



    std::vector<Hound> hounds;
    ... // fill hounds
    std::sort(hounds.begin(), hounds.end(), cmp);


    And you can even imagine cases where Type1 and Type2 are not the same, eg.:



    bool cmp(const Animal &a, const Dog &b); // Special compare to make dogs at the
    // front because dogs are better!
    etc ...



    The types Type1 (Animal) and Type2 (Dog) must be such that an object of type RandomIt (Hound) can be dereferenced and then implicitly converted to both of them. ​Which is true.







    share|improve this answer














    Its not about what is stored in the array. It is about what the comparator function is. Take for example this:



    struct Animal ;
    struct Cat : Animal ;
    struct Dog : Animal ;
    struct Hound : Dog ;

    bool cmp(const Animal &a, const Animal &b);


    Even if you have a list of Dogs, Cats or Hounds you can still sort them with the function cmp because they are all implicitly convertible. ie.



    std::vector<Hound> hounds;
    ... // fill hounds
    std::sort(hounds.begin(), hounds.end(), cmp);


    And you can even imagine cases where Type1 and Type2 are not the same, eg.:



    bool cmp(const Animal &a, const Dog &b); // Special compare to make dogs at the
    // front because dogs are better!
    etc ...



    The types Type1 (Animal) and Type2 (Dog) must be such that an object of type RandomIt (Hound) can be dereferenced and then implicitly converted to both of them. ​Which is true.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 57 mins ago

























    answered 1 hour ago









    Fantastic Mr Fox

    17.8k1765125




    17.8k1765125











    • but still both arguments of comparator function is Animal. I want to see when we can have 2 different types in comparator function as standard.
      – Afshin
      1 hour ago










    • @Afshin I have added an example of this.
      – Fantastic Mr Fox
      1 hour ago










    • @Afshin its not about restricting to the most typical use case only but to have maximum possible generality (allowing differnt types for the comparator) while still guaranteeing correctness (the types can be implicitly converted). It would be a major annoyance if you had a function that can compare the instances in the container, but you could not use it for sorting the container
      – user463035818
      57 mins ago











    • Hm....This is true...But after a little thinking, I thought maybe this can be used for sorting in multiple-inherience. But maybe i'm wrong.
      – Afshin
      49 mins ago










    • added a multiple inherience example to my port :)
      – Afshin
      36 mins ago
















    • but still both arguments of comparator function is Animal. I want to see when we can have 2 different types in comparator function as standard.
      – Afshin
      1 hour ago










    • @Afshin I have added an example of this.
      – Fantastic Mr Fox
      1 hour ago










    • @Afshin its not about restricting to the most typical use case only but to have maximum possible generality (allowing differnt types for the comparator) while still guaranteeing correctness (the types can be implicitly converted). It would be a major annoyance if you had a function that can compare the instances in the container, but you could not use it for sorting the container
      – user463035818
      57 mins ago











    • Hm....This is true...But after a little thinking, I thought maybe this can be used for sorting in multiple-inherience. But maybe i'm wrong.
      – Afshin
      49 mins ago










    • added a multiple inherience example to my port :)
      – Afshin
      36 mins ago















    but still both arguments of comparator function is Animal. I want to see when we can have 2 different types in comparator function as standard.
    – Afshin
    1 hour ago




    but still both arguments of comparator function is Animal. I want to see when we can have 2 different types in comparator function as standard.
    – Afshin
    1 hour ago












    @Afshin I have added an example of this.
    – Fantastic Mr Fox
    1 hour ago




    @Afshin I have added an example of this.
    – Fantastic Mr Fox
    1 hour ago












    @Afshin its not about restricting to the most typical use case only but to have maximum possible generality (allowing differnt types for the comparator) while still guaranteeing correctness (the types can be implicitly converted). It would be a major annoyance if you had a function that can compare the instances in the container, but you could not use it for sorting the container
    – user463035818
    57 mins ago





    @Afshin its not about restricting to the most typical use case only but to have maximum possible generality (allowing differnt types for the comparator) while still guaranteeing correctness (the types can be implicitly converted). It would be a major annoyance if you had a function that can compare the instances in the container, but you could not use it for sorting the container
    – user463035818
    57 mins ago













    Hm....This is true...But after a little thinking, I thought maybe this can be used for sorting in multiple-inherience. But maybe i'm wrong.
    – Afshin
    49 mins ago




    Hm....This is true...But after a little thinking, I thought maybe this can be used for sorting in multiple-inherience. But maybe i'm wrong.
    – Afshin
    49 mins ago












    added a multiple inherience example to my port :)
    – Afshin
    36 mins ago




    added a multiple inherience example to my port :)
    – Afshin
    36 mins ago












    up vote
    8
    down vote














    But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.




    You can't have two different types in an array. The comparator doesn't suggest it's possible. It's specified like that simply because:



    1. The code can be well formed when the types are not the same.

    2. Demanding the same type is a restriction that serves little to no purpose.

    So the specification offers a looser contract than is "obvious", in order to help our code be more flexible if needed. As a toy example, say we have this comparator laying around:



    auto cmp(int a, long b) -> bool return a < b; 


    Why prevent us from using this perfectly legal (albeit silly) function to sort an array of integers?






    share|improve this answer




















    • I found it possible with multiple inheritance as I added in my post. if your array objects has multiple inheritance, you can compare 2 different types of parent in your comparator. But I agree it will be silly in normal conditions.
      – Afshin
      33 mins ago











    • @Afshin - Yes, that's one example. The point I was trying to make is that even without an example in mind, the specification can be reasoned and defined loosely to allow for flexibility in general.
      – StoryTeller
      29 mins ago














    up vote
    8
    down vote














    But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.




    You can't have two different types in an array. The comparator doesn't suggest it's possible. It's specified like that simply because:



    1. The code can be well formed when the types are not the same.

    2. Demanding the same type is a restriction that serves little to no purpose.

    So the specification offers a looser contract than is "obvious", in order to help our code be more flexible if needed. As a toy example, say we have this comparator laying around:



    auto cmp(int a, long b) -> bool return a < b; 


    Why prevent us from using this perfectly legal (albeit silly) function to sort an array of integers?






    share|improve this answer




















    • I found it possible with multiple inheritance as I added in my post. if your array objects has multiple inheritance, you can compare 2 different types of parent in your comparator. But I agree it will be silly in normal conditions.
      – Afshin
      33 mins ago











    • @Afshin - Yes, that's one example. The point I was trying to make is that even without an example in mind, the specification can be reasoned and defined loosely to allow for flexibility in general.
      – StoryTeller
      29 mins ago












    up vote
    8
    down vote










    up vote
    8
    down vote










    But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.




    You can't have two different types in an array. The comparator doesn't suggest it's possible. It's specified like that simply because:



    1. The code can be well formed when the types are not the same.

    2. Demanding the same type is a restriction that serves little to no purpose.

    So the specification offers a looser contract than is "obvious", in order to help our code be more flexible if needed. As a toy example, say we have this comparator laying around:



    auto cmp(int a, long b) -> bool return a < b; 


    Why prevent us from using this perfectly legal (albeit silly) function to sort an array of integers?






    share|improve this answer













    But I still cannot get it exactly how we can have 2 different types in a single array when we try to sort it.




    You can't have two different types in an array. The comparator doesn't suggest it's possible. It's specified like that simply because:



    1. The code can be well formed when the types are not the same.

    2. Demanding the same type is a restriction that serves little to no purpose.

    So the specification offers a looser contract than is "obvious", in order to help our code be more flexible if needed. As a toy example, say we have this comparator laying around:



    auto cmp(int a, long b) -> bool return a < b; 


    Why prevent us from using this perfectly legal (albeit silly) function to sort an array of integers?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 1 hour ago









    StoryTeller

    86.5k12174241




    86.5k12174241











    • I found it possible with multiple inheritance as I added in my post. if your array objects has multiple inheritance, you can compare 2 different types of parent in your comparator. But I agree it will be silly in normal conditions.
      – Afshin
      33 mins ago











    • @Afshin - Yes, that's one example. The point I was trying to make is that even without an example in mind, the specification can be reasoned and defined loosely to allow for flexibility in general.
      – StoryTeller
      29 mins ago
















    • I found it possible with multiple inheritance as I added in my post. if your array objects has multiple inheritance, you can compare 2 different types of parent in your comparator. But I agree it will be silly in normal conditions.
      – Afshin
      33 mins ago











    • @Afshin - Yes, that's one example. The point I was trying to make is that even without an example in mind, the specification can be reasoned and defined loosely to allow for flexibility in general.
      – StoryTeller
      29 mins ago















    I found it possible with multiple inheritance as I added in my post. if your array objects has multiple inheritance, you can compare 2 different types of parent in your comparator. But I agree it will be silly in normal conditions.
    – Afshin
    33 mins ago





    I found it possible with multiple inheritance as I added in my post. if your array objects has multiple inheritance, you can compare 2 different types of parent in your comparator. But I agree it will be silly in normal conditions.
    – Afshin
    33 mins ago













    @Afshin - Yes, that's one example. The point I was trying to make is that even without an example in mind, the specification can be reasoned and defined loosely to allow for flexibility in general.
    – StoryTeller
    29 mins ago




    @Afshin - Yes, that's one example. The point I was trying to make is that even without an example in mind, the specification can be reasoned and defined loosely to allow for flexibility in general.
    – StoryTeller
    29 mins ago










    up vote
    1
    down vote














    But I still cannot get it exactly how we can have 2 different types in a single array




    You cannot have two different types in a single array.



    An array can have objects of only single type. But that single type must be implicitly convertible to both argument types of cmp.




    Is ti possible that someone provide a small example with different types for std::sort's comparator function?




    Here you go:



    int arr = 1, 2, 3, 0;
    auto cmp = (const int &a, const long &b)
    return a < b;
    ;
    std::sort(std::begin(arr), std::end(arr), cmp);


    Note the two different arguments of cmp. This is just a minimal example, which is technically correct, but admittedly nonsensical. Frankly, I've never encountered a case where it would be useful to have different types for the arguments of a comparison function.






    share|improve this answer


























      up vote
      1
      down vote














      But I still cannot get it exactly how we can have 2 different types in a single array




      You cannot have two different types in a single array.



      An array can have objects of only single type. But that single type must be implicitly convertible to both argument types of cmp.




      Is ti possible that someone provide a small example with different types for std::sort's comparator function?




      Here you go:



      int arr = 1, 2, 3, 0;
      auto cmp = (const int &a, const long &b)
      return a < b;
      ;
      std::sort(std::begin(arr), std::end(arr), cmp);


      Note the two different arguments of cmp. This is just a minimal example, which is technically correct, but admittedly nonsensical. Frankly, I've never encountered a case where it would be useful to have different types for the arguments of a comparison function.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote










        But I still cannot get it exactly how we can have 2 different types in a single array




        You cannot have two different types in a single array.



        An array can have objects of only single type. But that single type must be implicitly convertible to both argument types of cmp.




        Is ti possible that someone provide a small example with different types for std::sort's comparator function?




        Here you go:



        int arr = 1, 2, 3, 0;
        auto cmp = (const int &a, const long &b)
        return a < b;
        ;
        std::sort(std::begin(arr), std::end(arr), cmp);


        Note the two different arguments of cmp. This is just a minimal example, which is technically correct, but admittedly nonsensical. Frankly, I've never encountered a case where it would be useful to have different types for the arguments of a comparison function.






        share|improve this answer















        But I still cannot get it exactly how we can have 2 different types in a single array




        You cannot have two different types in a single array.



        An array can have objects of only single type. But that single type must be implicitly convertible to both argument types of cmp.




        Is ti possible that someone provide a small example with different types for std::sort's comparator function?




        Here you go:



        int arr = 1, 2, 3, 0;
        auto cmp = (const int &a, const long &b)
        return a < b;
        ;
        std::sort(std::begin(arr), std::end(arr), cmp);


        Note the two different arguments of cmp. This is just a minimal example, which is technically correct, but admittedly nonsensical. Frankly, I've never encountered a case where it would be useful to have different types for the arguments of a comparison function.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 59 mins ago

























        answered 1 hour ago









        user2079303

        72.6k552110




        72.6k552110



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52984999%2fdifferent-types-for-stdsort-comparator-in-c%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            One-line joke