Skip to content

Commit d0e9b32

Browse files
committed
Implement some C++11 functionality
Containers now support move ctor and assignment, emplace, and initlists
1 parent b932765 commit d0e9b32

31 files changed

+833
-576
lines changed

bvt/bvt04.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class A {
99
public:
1010
A (void)
1111
{ cout << "A::A\n"; }
12+
A (int a)
13+
{ cout << "A::A(" << a << ")\n"; }
1214
A (const A&)
1315
{ cout << "Copy A::A\n"; }
1416
const A& operator= (const A&)
@@ -63,6 +65,18 @@ void TestVector (void)
6365
ctv.assign (3, a);
6466
ctv.pop_back();
6567
cout << "Class insertion testing successful\n";
68+
#if HAVE_CPP11
69+
v = vector<int>(vector<int>({1,2,3,4,5,6,7,8}));
70+
v.insert (v.begin()+3, {11,12,13});
71+
v.emplace (v.begin()+7, 22);
72+
cout << '{';
73+
for (auto i : v)
74+
cout << ' ' << i;
75+
cout << " }" << endl;
76+
ctv.emplace_back (15);
77+
#else
78+
cout << "{ 1 2 3 11 12 13 4 22 5 6 7 8 }\nA::A(15)\nA::~A\n";
79+
#endif
6680
}
6781

6882
StdBvtMain (TestVector)

bvt/bvt04.std

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ A::A
1818
A::operator=
1919
A::operator=
2020
A::operator=
21+
A::~A
2122
Class insertion testing successful
23+
{ 1 2 3 11 12 13 4 22 5 6 7 8 }
24+
A::A(15)
2225
A::~A
2326
A::~A
2427
A::~A

bvt/bvt11.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ void PrintVector (const int* first, const int* last)
1515

1616
void TestSetAndMultiset (void)
1717
{
18+
#if HAVE_CPP11
19+
set<int> v (set<int>({ 1, 8, 2, 3, 1, 1, 4, 6, 1, 3, 4 }));
20+
multiset<int> mv (multiset<int>({ 1, 8, 9, 2, 3, 4, 6, 1, 3, 4 }));
21+
v.emplace (9);
22+
mv.emplace (1);
23+
mv.emplace (1);
24+
#else
1825
const int vv[] = { 1, 8, 9, 2, 3, 1, 1, 4, 6, 1, 3, 4 };
1926
set<int> v (VectorRange (vv));
2027
multiset<int> mv (VectorRange (vv));
28+
#endif
2129
cout << "set:\t\t";
2230
PrintVector (v.begin(), v.end());
2331
cout << "erase(3):\t";

bvt/bvt14.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@ void TestMap (void)
1717
months["june"] = 30;
1818
months["july"] = 31;
1919
months["august"] = 31;
20+
#if HAVE_CPP11
21+
months.insert ({
22+
{"september",30},
23+
{"october",31},
24+
{"november",30},
25+
{"december",31}
26+
});
27+
#else
2028
months["september"] = 30;
2129
months["october"] = 31;
2230
months["november"] = 30;
2331
months["december"] = 31;
32+
#endif
2433

2534
const monthmap_t& cmonths = months;
2635
cout << "There are " << cmonths["january"] << " days in january." << endl;
27-
cout << "There are " << cmonths["september"] << " days in september." << endl;
36+
cout << "There are " << cmonths.at("september") << " days in september." << endl;
2837
cout << "There are " << cmonths["december"] << " days in december." << endl;
2938
monthmap_t::const_iterator found_may = months.find ("may");
3039
cout << found_may->first << " found at index " << found_may - months.begin() << endl;
@@ -49,7 +58,11 @@ void TestMap (void)
4958
cout << endl;
5059

5160
mcopy = months;
61+
#if HAVE_CPP11
62+
monthmap_t::iterator frob = mcopy.emplace_hint (mcopy.begin(), make_pair (string("frobuary"), 42));
63+
#else
5264
monthmap_t::iterator frob = mcopy.insert (mcopy.begin(), make_pair (string("frobuary"), 42));
65+
#endif
5366
cout << "After inserting " << frob->first << "," << frob->second << ":" << endl;
5467
for (i = mcopy.begin(); i < mcopy.end(); ++ i)
5568
cout << i->first << " ";

bvt/bvt15.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ void TestMultiMap (void)
2323
employees.insert (make_pair (27000, string("Jim")));
2424
employees.insert (make_pair (99000, string("BigBoss")));
2525
employees.insert (make_pair (47000, string("Gail")));
26+
#if HAVE_CPP11
27+
employees.emplace (make_pair (15000, string("Dumb")));
28+
employees.insert ({47000, "Barbara"});
29+
#else
2630
employees.insert (make_pair (15000, string("Dumb")));
2731
employees.insert (make_pair (47000, string("Barbara")));
32+
#endif
2833
employees.insert (make_pair (47000, string("Mary")));
2934

3035
cout << "As-inserted listing:\n";

bvt/bvt18.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,29 @@ void TestTuple (const char* ctrType)
1212
cout << "Testing " << ctrType << endl;
1313
cout << "================================================" << endl;
1414
assert (N <= 8);
15-
T pt1v[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
16-
T increment;
15+
#if HAVE_CPP11
16+
tuple<N,T> pt1 ({1,2,3,4,5,6,7,8});
17+
tuple<N,T> pt2;
18+
pt2 = {4,4,4,4};
19+
pt2 += {1,2,3,4};
20+
#else
21+
T pt1v[12] = { 1, 2, 3, 4, 5, 6, 7, 8 };
1722
tuple<N,T> pt1 (pt1v);
18-
tuple<N,T> pt2 (5, 6, 7, 8);
19-
increment = pt1v[2];
23+
tuple<N,T> pt2 (&pt1v[4]);
24+
#endif
2025

2126
cout << "pt1:\t\t\tsize = " << pt1.size() << ", value = " << pt1 << endl;
2227
cout << "pt2:\t\t\t" << pt2 << endl;
2328
iota (pt2.begin(), pt2.end(), 10);
2429
cout << "pt2:\t\t\t" << pt2 << endl;
2530

26-
pt1 *= increment;
31+
pt1 *= 3;
2732
cout << "pt1 *= 3:\t\t" << pt1 << endl;
28-
pt1 /= increment;
33+
pt1 /= 3;
2934
cout << "pt1 /= 3:\t\t" << pt1 << endl;
30-
pt1 += increment;
35+
pt1 += 3;
3136
cout << "pt1 += 3:\t\t" << pt1 << endl;
32-
pt1 -= increment;
37+
pt1 -= 3;
3338
cout << "pt1 -= 3:\t\t" << pt1 << endl;
3439

3540
pt1 *= pt2;

cmemlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class cmemlink {
6464
inline size_type max_size (void) const { return (size()); }
6565
inline size_type readable_size (void) const { return (size()); }
6666
inline bool empty (void) const { return (!size()); }
67+
inline const_pointer data (void) const { return (m_Data); }
6768
inline const_pointer cdata (void) const { return (m_Data); }
6869
inline iterator begin (void) const { return (iterator (cdata())); }
6970
inline iterator iat (size_type i) const { assert (i <= size()); return (begin() + i); }

config.h.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@
5454
#define DLL_LOCAL
5555
#define INLINE
5656
#endif
57-
#if __cplusplus >= 201103L && (!__GNUC__ || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
57+
#if __cplusplus >= 201103L && (!__GNUC__ || (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 2)) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
5858
#define HAVE_CPP11 1
5959
#endif
6060
#if !HAVE_CPP11
6161
#define noexcept throw()
62+
#define constexpr
6263
#endif
6364
#if __GNUC__ >= 3 && (__i386__ || __x86_64__)
6465
/// GCC 3+ supports the prefetch directive, which some CPUs use to improve caching

configure

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ stdlib.h string.h time.h unistd.h math.h stdlib.h sys/mman.h";
6262
LIBS="supc++ gcc_eh SystemStubs"
6363

6464
# First pair is used if nothing matches
65-
PROGS="CC=gcc CC=cc CXX=g++ CXX=c++ DOXYGEN=doxygen LD=ld AR=ar RANLIB=ranlib RANLIB=touch INSTALL=install"
65+
PROGS="CC=gcc CC=clang CC=cc CXX=g++ CXX=clang++ CXX=c++ DOXYGEN=doxygen LD=ld AR=ar RANLIB=ranlib RANLIB=touch INSTALL=install"
6666

6767
# Environment variables
6868
ENVIRONS="CXXFLAGS LDFLAGS"
@@ -231,7 +231,7 @@ int main (void)
231231
printf (SET(CPU_HAS_%s,1), s_CpuCaps[i].name);
232232
#if __GNUC__ >= 3
233233
printf ("s/ \\?@PROCESSOR_OPTS@/");
234-
#if __clang_major__ >= 3 || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
234+
#if (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 2)) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
235235
printf (" -std=c++11 -march=native");
236236
#else
237237
if (caps & (1<<23)) printf (" -mmmx");

memblock.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void memblock::shrink_to_fit (void)
112112
}
113113

114114
/// Shifts the data in the linked block from \p start to \p start + \p n.
115-
memblock::iterator memblock::insert (iterator start, size_type n)
115+
memblock::iterator memblock::insert (const_iterator start, size_type n)
116116
{
117117
const uoff_t ip = start - begin();
118118
assert (ip <= size());
@@ -122,7 +122,7 @@ memblock::iterator memblock::insert (iterator start, size_type n)
122122
}
123123

124124
/// Shifts the data in the linked block from \p start + \p n to \p start.
125-
memblock::iterator memblock::erase (iterator start, size_type n)
125+
memblock::iterator memblock::erase (const_iterator start, size_type n)
126126
{
127127
const uoff_t ep = start - begin();
128128
assert (ep + n <= size());

0 commit comments

Comments
 (0)