This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: libstdc++ __inplace_stable_sort build failure


A few notes:

intPair data[(int64_t)1e7];

is unusual and possibly dangerous because of floating point inaccuracy,
I'd simply prefer

intPair data[1000000];

You should also make lhs and rhs references:

bool less(const intPair& lhs, const intPair& rhs) {

Also use std::end to completely avoid that C-Style macro:

std::stable_sort(std::begin (data), std::end (data), less);

The std::begin is optional, but makes it easier to later change the type
of "data" (e.g. to std::vector).

On 30.04.2018 15:08, nsajko@gmail.com wrote:
> The following program:
>
>
> #include <algorithm>
>
> #define ARRAY_LEN(arr) (sizeof(arr) / sizeof((arr)[0]))
>
> typedef struct {
>         int64_t a;
>         int64_t b;
> } intPair;
> intPair data[(int64_t)1e7];
>
> bool
> less(const intPair lhs, const intPair rhs) {
>         return lhs.a < rhs.a;
> }
>
> int
> main(void) {
>         std::stable_sort(data, data + ARRAY_LEN(data), less);
>         std::__inplace_stable_sort(data, data + ARRAY_LEN(data), less);
>         return 0;
> }
>
>
> gives the following error (notice that the error is only for
> __inplace_stable_sort, not for stable_sort):
>
>
> In file included from /usr/include/c++/7.3.1/algorithm:62:0,
>                  from cxxStableSortSimpler.cxx:1:
> /usr/include/c++/7.3.1/bits/stl_algo.h: In instantiation of ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = intPair*; _Compare = bool (*)(intPair, intPair)]’:
> /usr/include/c++/7.3.1/bits/stl_algo.h:2766:25:   required from ‘void std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = intPair*; _Compare = bool (*)(intPair, intPair)]’
> cxxStableSortSimpler.cxx:19:63:   required from here
> /usr/include/c++/7.3.1/bits/stl_algo.h:1847:14: error: could not convert ‘__i’ from ‘intPair*’ to ‘intPair’
>     if (__comp(__i, __first))
>         ~~~~~~^~~~~~~~~~~~~~
> /usr/include/c++/7.3.1/bits/stl_algo.h: In instantiation of ‘void std::__merge_without_buffer(_BidirectionalIterator, _BidirectionalIterator, _BidirectionalIterator, _Distance, _Distance, _Compare) [with _BidirectionalIterator = intPair*; _Distance = long int; _Compare = bool (*)(intPair, intPair)]’:
> /usr/include/c++/7.3.1/bits/stl_algo.h:2772:34:   required from ‘void std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = intPair*; _Compare = bool (*)(intPair, intPair)]’
> cxxStableSortSimpler.cxx:19:63:   required from here
> /usr/include/c++/7.3.1/bits/stl_algo.h:2487:14: error: could not convert ‘__middle’ from ‘intPair*’ to ‘intPair’
>     if (__comp(__middle, __first))
>         ~~~~~~^~~~~~~~~~~~~~~~~~~
>
>
> So how does one use __inplace_stable_sort correctly?
>
> Regards,
> Neven Sajko
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]