<html>
<head>
<base href="https://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - stl::sort cannot sort globally declared vector of struct type"
href="https://llvm.org/bugs/show_bug.cgi?id=22993">22993</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>stl::sort cannot sort globally declared vector of struct type
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.5
</td>
</tr>
<tr>
<th>Hardware</th>
<td>Macintosh
</td>
</tr>
<tr>
<th>OS</th>
<td>MacOS X
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>insik92@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Following source code should sort EDGE structure according to its member c in
ascending order.
Compiling it with clang++ with default option (clang++ ./testsort.cpp) results
partially sorted edges. Below is the example.
i: 0 val: u: 6 v: 6 c: 0
i: 1 val: u: 26 v: 26 c: 13
i: 2 val: u: 74 v: 74 c: 16
i: 3 val: u: 84 v: 84 c: 43
i: 4 val: u: 67 v: 67 c: 76
i: 5 val: u: 82 v: 82 c: 83
i: 6 val: u: 99 v: 99 c: 85
i: 7 val: u: 38 v: 38 c: 86
i: 8 val: u: 90 v: 90 c: 95
i: 9 val: u: 45 v: 45 c: 107
i: 10 val: u: 54 v: 54 c: 117
i: 11 val: u: 22 v: 22 c: 118
i: 12 val: u: 56 v: 56 c: 137
i: 13 val: u: 77 v: 77 c: 142
i: 14 val: u: 2 v: 2 c: 147
i: 15 val: u: 61 v: 61 c: 162
i: 16 val: u: 7 v: 7 c: 164
i: 17 val: u: 30 v: 30 c: 185
i: 18 val: u: 1 v: 1 c: 213
i: 19 val: u: 15 v: 15 c: 218
i: 20 val: u: 49 v: 49 c: 222
i: 21 val: u: 80 v: 80 c: 242
i: 22 val: u: 64 v: 64 c: 253
i: 23 val: u: 86 v: 86 c: 255
i: 24 val: u: 25 v: 25 c: 275
i: 25 val: u: 62 v: 62 c: 280
i: 26 val: u: 78 v: 78 c: 282
i: 27 val: u: 12 v: 12 c: 287
i: 28 val: u: 51 v: 51 c: 288
i: 29 val: u: 52 v: 52 c: 289
i: 30 val: u: 19 v: 19 c: 312
i: 31 val: u: 48 v: 48 c: 332
i: 32 val: u: 92 v: 92 c: 343
i: 33 val: u: 50 v: 50 c: 355
i: 34 val: u: 20 v: 20 c: 345
i: 35 val: u: 17 v: 17 c: 347
i: 36 val: u: 47 v: 47 c: 350
i: 37 val: u: 94 v: 94 c: 351
i: 38 val: u: 58 v: 58 c: 355
i: 39 val: u: 44 v: 44 c: 378
i: 40 val: u: 35 v: 35 c: 437
i: 41 val: u: 33 v: 33 c: 670
i: 42 val: u: 68 v: 68 c: 385
i: 43 val: u: 5 v: 5 c: 399
i: 44 val: u: 4 v: 4 c: 413
i: 45 val: u: 60 v: 60 c: 472
i: 46 val: u: 10 v: 10 c: 475
i: 47 val: u: 73 v: 73 c: 477
i: 48 val: u: 23 v: 23 c: 484
i: 49 val: u: 14 v: 14 c: 489
i: 50 val: u: 98 v: 98 c: 533
i: 51 val: u: 53 v: 53 c: 534
i: 52 val: u: 89 v: 89 c: 547
i: 53 val: u: 29 v: 29 c: 559
i: 54 val: u: 91 v: 91 c: 580
i: 55 val: u: 66 v: 66 c: 584
i: 56 val: u: 85 v: 85 c: 597
i: 57 val: u: 75 v: 75 c: 601
i: 58 val: u: 69 v: 69 c: 621
i: 59 val: u: 59 v: 59 c: 621
i: 60 val: u: 63 v: 63 c: 647
i: 61 val: u: 24 v: 24 c: 651
i: 62 val: u: 31 v: 31 c: 652
i: 63 val: u: 9 v: 9 c: 692
i: 64 val: u: 18 v: 18 c: 702
i: 65 val: u: 3 v: 3 c: 711
i: 66 val: u: 0 v: 0 c: 730
i: 67 val: u: 97 v: 97 c: 731
i: 68 val: u: 57 v: 57 c: 733
i: 69 val: u: 37 v: 37 c: 738
i: 70 val: u: 11 v: 11 c: 744
i: 71 val: u: 72 v: 72 c: 866
i: 72 val: u: 88 v: 88 c: 752
i: 73 val: u: 95 v: 95 c: 774
i: 74 val: u: 70 v: 70 c: 788
i: 75 val: u: 32 v: 32 c: 790
i: 76 val: u: 79 v: 79 c: 822
i: 77 val: u: 65 v: 65 c: 842
i: 78 val: u: 13 v: 13 c: 842
i: 79 val: u: 46 v: 46 c: 844
i: 80 val: u: 43 v: 43 c: 847
i: 81 val: u: 36 v: 36 c: 851
i: 82 val: u: 96 v: 96 c: 862
i: 83 val: u: 93 v: 93 c: 879
i: 84 val: u: 28 v: 28 c: 895
i: 85 val: u: 16 v: 16 c: 896
i: 86 val: u: 55 v: 55 c: 898
i: 87 val: u: 87 v: 87 c: 959
i: 88 val: u: 42 v: 42 c: 916
i: 89 val: u: 27 v: 27 c: 918
i: 90 val: u: 8 v: 8 c: 923
i: 91 val: u: 71 v: 71 c: 930
i: 92 val: u: 39 v: 39 c: 945
i: 93 val: u: 81 v: 81 c: 949
i: 94 val: u: 34 v: 34 c: 953
i: 95 val: u: 76 v: 76 c: 953
i: 96 val: u: 40 v: 40 c: 967
i: 97 val: u: 83 v: 83 c: 969
i: 98 val: u: 41 v: 41 c: 993
i: 99 val: u: 21 v: 21 c: 995
However the same code compiled with GNU g++-4.9 gives correctly sorted result.
---------Source Code------
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>
struct EDGE{
int u, v;
int c;
bool operator () (EDGE const& a, EDGE const& b) const {
return a.c <= b.c;
}
} ;
std::ostream& operator << (std::ostream &out, EDGE const& data){
out << "u: " << data.u << " v: " << data.v << " c: " << data.c;
return out;
}
std::vector<EDGE> edges;
int main(){
int n = 100;
std::srand(std::time(0)); // use cur time for seed.
for(int i=0;i<n;i++){
EDGE tmp;
tmp.u = i;
tmp.v = i;
tmp.c = std::rand()%1000;
edges.push_back(tmp);
}
std::sort(edges.begin(), edges.end(), EDGE());
for (int i=0; i<edges.size();i++){
std::cout << "i: " << i << " val: " << edges[i] << '\n';
}
return 0;
}</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>