-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull_iterators.cpp
107 lines (87 loc) · 2.15 KB
/
null_iterators.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// N3644 Null Forward Iterators
// Discussion: gcc.gnu.org/ml/libstdc++/2013-11/msg00076.html
// $HOME/bin/bin/g++ -std=c++1y -o null_iterators null_iterators.cpp
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <cassert>
#include <set>
template<typename _Tp, template<typename, typename> class _Cont>
void
test2(_Cont<_Tp, std::allocator<_Tp>>)
{
typename _Cont<_Tp, std::allocator<_Tp>>::iterator ci1{};
typename _Cont<_Tp, std::allocator<_Tp>>::iterator ci2{};
assert(ci1 == ci2);
}
template<typename _Tp, template<typename, typename, typename> class _Cont>
void
test3(_Cont<_Tp, std::less<_Tp>, std::allocator<_Tp>>)
{
typename _Cont<_Tp, std::less<_Tp>, std::allocator<_Tp>>::iterator ci1{};
typename _Cont<_Tp, std::less<_Tp>, std::allocator<_Tp>>::iterator ci2{};
assert(ci1 == ci2);
}
struct A
{
virtual std::vector<int>::const_iterator
begin() const
{ return std::vector<int>::const_iterator{}; }
virtual std::vector<int>::const_iterator
end() const
{ return std::vector<int>::const_iterator{}; }
};
A __gA{};
struct B
: public A
{
virtual std::vector<int>::const_iterator
begin() const override
{ return v.cbegin(); }
virtual std::vector<int>::const_iterator
end() const override
{ return v.cend(); }
std::vector<int> v;
};
B __gB{};
const A&
get_an_A(int i)
{
if (i % 2)
return __gB;
else
return __gA;
}
void
test0()
{
const A& ar = get_an_A(1);
const A& as = get_an_A(2);
for (int x : ar);
// do_something(x);
//do_something_else(ar);
}
int
main()
{
std::vector<int>::iterator vi1{};
std::vector<int>::iterator vi2{};
assert(vi1 == vi2);
std::vector<double>::iterator vd1{};
std::vector<double>::iterator vd2{};
assert(vd1 == vd2);
std::deque<int>::iterator di1{};
std::deque<int>::iterator di2{};
assert(di1 == di2);
std::deque<double>::iterator dd1{};
std::deque<double>::iterator dd2{};
assert(dd1 == dd2);
test2(std::vector<long>{});
test2(std::deque<long>{});
test2(std::deque<long>{});
test2(std::list<long>{});
test2(std::forward_list<long>{});
test3(std::set<long>{});
//vi1 == vd1; // Won't compile.
}