EDN Admin
Well-known member
I get a compile error on the code below due to the member variable alloc being private.<br/>
It compiles with gcc 4.2.1 as well as Comeau C++ 4.3.10.1 ( http://www.comeaucomputing.com/tryitout/). http://www.comeaucomputing.com/tryitout/).
The code is from the book STL Tutorial and Reference Guide, Second Edition<br/>
by David R. Musser, Gillmer J. Derge and Atul Saini.<br/>
It can also be found on the web site http://www.cs.rpi.edu/~musser/stl-book/index_3.html.
http://www.cs.rpi.edu/~musser/stl-book/index_3.html.
This is a custom allocator. The error shows in <vector> when instantiating an instance of vector<int,logging_allocator<int>>.
Is this a compiler bug? Or, is the code flawed?
// ---------- File logalloc.h ----------
#include <memory><br/>
#include <iostream><br/>
using namespace std;
template <typename T, typename Allocator = allocator<T> ><br/>
class logging_allocator<br/>
{<br/>
private:<br/>
Allocator alloc;
public:<br/>
typedef typename Allocator::size_type size_type;<br/>
typedef typename Allocator::difference_type difference_type;<br/>
typedef typename Allocator:ointer pointer;<br/>
typedef typename Allocator::const_pointer const_pointer;<br/>
typedef typename Allocator::reference reference;<br/>
typedef typename Allocator::const_reference const_reference;<br/>
typedef typename Allocator::value_type value_type;<br/>
template <typename U> struct rebind {<br/>
typedef logging_allocator<U, <br/>
typename Allocator::template rebind<U>:ther> other;<br/>
};
logging_allocator() {}<br/>
logging_allocator(const logging_allocator& x)<br/>
: alloc(x.alloc) {}<br/>
template <typename U><br/>
logging_allocator(const logging_allocator<U, <br/>
typename Allocator::template rebind<U>:ther>& x)<br/>
: alloc(x.alloc) {}<br/>
~logging_allocator() {}<br/>
<br/>
pointer address(reference x) const {<br/>
return alloc.address(x); <br/>
}<br/>
const_pointer address(const_reference x) const { <br/>
return alloc.address(x); <br/>
}<br/>
size_type max_size() const { return alloc.max_size(); }<br/>
void construct(pointer p, const value_type& val) { <br/>
alloc.construct(p, val);<br/>
}<br/>
void destroy(pointer p) { alloc.destroy(p); }<br/>
<br/>
pointer allocate(size_type n, const void* hint = 0) {<br/>
ios::fmtflags flags = cerr.flags();
cerr << "allocate(" << n << ", " <br/>
<< hex << hint << dec << ") = ";<br/>
pointer result = alloc.allocate(n, hint);<br/>
cerr << hex << result << dec << endl;
cerr.setf(flags);<br/>
return result;<br/>
}
void deallocate(pointer p, size_type n) {<br/>
ios::fmtflags flags = cerr.flags();
cerr << "deallocate(" << hex << p << dec << ", "<br/>
<< n << ")" << endl;<br/>
alloc.deallocate(p, n);
cerr.setf(flags);<br/>
}<br/>
};
template <typename T, typename Allocator1, <br/>
typename U, typename Allocator2><br/>
bool operator==(const logging_allocator<T, Allocator1>& x,<br/>
const logging_allocator<U, Allocator2>& y) {<br/>
return x.alloc == y.alloc; <br/>
}
template <typename T, typename Allocator1, <br/>
typename U, typename Allocator2><br/>
bool operator!=(const logging_allocator<T, Allocator1>& x,<br/>
const logging_allocator<U, Allocator2>& y) {<br/>
return x.alloc != y.alloc; <br/>
}
// ---------- File ex24-01.cpp ----------<br/>
// Demonstrating use of a custom allocator<br/>
#include <iostream><br/>
#include <vector><br/>
using namespace std;
#include "logalloc.h"
int main()<br/>
{<br/>
cout << "Demonstrating use of a custom allocator." << endl;
cout << "-- Default allocator --" << endl;<br/>
vector<int> v1;<br/>
for (int i = 0; i < 10; ++i) {<br/>
cout << " Inserting " << i << endl;<br/>
v1.push_back(i);<br/>
}<br/>
cout << "-- Done. --" << endl;
cout << "n-- Custom allocator --" << endl;<br/>
vector<int, logging_allocator<int> > v2;<br/>
for (int i = 0; i < 10; ++i) {<br/>
cout << " Inserting " << i << endl;<br/>
v2.push_back(i);<br/>
}<br/>
cout << "-- Done. --" << endl;
cout << "n-- Custom allocator with reserve --" << endl;<br/>
vector<int, logging_allocator<int> > v3;<br/>
v3.reserve(10);<br/>
for (int i = 0; i < 10; ++i) {<br/>
cout << " Inserting " << i << endl;<br/>
v3.push_back(i);<br/>
}<br/>
cout << "-- Done. --" << endl;
return 0;<br/>
}
View the full article
It compiles with gcc 4.2.1 as well as Comeau C++ 4.3.10.1 ( http://www.comeaucomputing.com/tryitout/). http://www.comeaucomputing.com/tryitout/).
The code is from the book STL Tutorial and Reference Guide, Second Edition<br/>
by David R. Musser, Gillmer J. Derge and Atul Saini.<br/>
It can also be found on the web site http://www.cs.rpi.edu/~musser/stl-book/index_3.html.
http://www.cs.rpi.edu/~musser/stl-book/index_3.html.
This is a custom allocator. The error shows in <vector> when instantiating an instance of vector<int,logging_allocator<int>>.
Is this a compiler bug? Or, is the code flawed?
// ---------- File logalloc.h ----------
#include <memory><br/>
#include <iostream><br/>
using namespace std;
template <typename T, typename Allocator = allocator<T> ><br/>
class logging_allocator<br/>
{<br/>
private:<br/>
Allocator alloc;
public:<br/>
typedef typename Allocator::size_type size_type;<br/>
typedef typename Allocator::difference_type difference_type;<br/>
typedef typename Allocator:ointer pointer;<br/>
typedef typename Allocator::const_pointer const_pointer;<br/>
typedef typename Allocator::reference reference;<br/>
typedef typename Allocator::const_reference const_reference;<br/>
typedef typename Allocator::value_type value_type;<br/>
template <typename U> struct rebind {<br/>
typedef logging_allocator<U, <br/>
typename Allocator::template rebind<U>:ther> other;<br/>
};
logging_allocator() {}<br/>
logging_allocator(const logging_allocator& x)<br/>
: alloc(x.alloc) {}<br/>
template <typename U><br/>
logging_allocator(const logging_allocator<U, <br/>
typename Allocator::template rebind<U>:ther>& x)<br/>
: alloc(x.alloc) {}<br/>
~logging_allocator() {}<br/>
<br/>
pointer address(reference x) const {<br/>
return alloc.address(x); <br/>
}<br/>
const_pointer address(const_reference x) const { <br/>
return alloc.address(x); <br/>
}<br/>
size_type max_size() const { return alloc.max_size(); }<br/>
void construct(pointer p, const value_type& val) { <br/>
alloc.construct(p, val);<br/>
}<br/>
void destroy(pointer p) { alloc.destroy(p); }<br/>
<br/>
pointer allocate(size_type n, const void* hint = 0) {<br/>
ios::fmtflags flags = cerr.flags();
cerr << "allocate(" << n << ", " <br/>
<< hex << hint << dec << ") = ";<br/>
pointer result = alloc.allocate(n, hint);<br/>
cerr << hex << result << dec << endl;
cerr.setf(flags);<br/>
return result;<br/>
}
void deallocate(pointer p, size_type n) {<br/>
ios::fmtflags flags = cerr.flags();
cerr << "deallocate(" << hex << p << dec << ", "<br/>
<< n << ")" << endl;<br/>
alloc.deallocate(p, n);
cerr.setf(flags);<br/>
}<br/>
};
template <typename T, typename Allocator1, <br/>
typename U, typename Allocator2><br/>
bool operator==(const logging_allocator<T, Allocator1>& x,<br/>
const logging_allocator<U, Allocator2>& y) {<br/>
return x.alloc == y.alloc; <br/>
}
template <typename T, typename Allocator1, <br/>
typename U, typename Allocator2><br/>
bool operator!=(const logging_allocator<T, Allocator1>& x,<br/>
const logging_allocator<U, Allocator2>& y) {<br/>
return x.alloc != y.alloc; <br/>
}
// ---------- File ex24-01.cpp ----------<br/>
// Demonstrating use of a custom allocator<br/>
#include <iostream><br/>
#include <vector><br/>
using namespace std;
#include "logalloc.h"
int main()<br/>
{<br/>
cout << "Demonstrating use of a custom allocator." << endl;
cout << "-- Default allocator --" << endl;<br/>
vector<int> v1;<br/>
for (int i = 0; i < 10; ++i) {<br/>
cout << " Inserting " << i << endl;<br/>
v1.push_back(i);<br/>
}<br/>
cout << "-- Done. --" << endl;
cout << "n-- Custom allocator --" << endl;<br/>
vector<int, logging_allocator<int> > v2;<br/>
for (int i = 0; i < 10; ++i) {<br/>
cout << " Inserting " << i << endl;<br/>
v2.push_back(i);<br/>
}<br/>
cout << "-- Done. --" << endl;
cout << "n-- Custom allocator with reserve --" << endl;<br/>
vector<int, logging_allocator<int> > v3;<br/>
v3.reserve(10);<br/>
for (int i = 0; i < 10; ++i) {<br/>
cout << " Inserting " << i << endl;<br/>
v3.push_back(i);<br/>
}<br/>
cout << "-- Done. --" << endl;
return 0;<br/>
}
View the full article